Understanding the basics of sensors in robotics

This is the FOURTH PROGRAM in your course of FUNDAMENTALS OF ROBOTICS. To use this program, you must use our robotics kit already supplied to you.

If you do not have this kit, then you can purchase our distance learning programme in robotics or contact us to join our regular batches of robotics.

Now read the following program carefully and work as per the given instructions, to enjoy your first program in robotics.

To use this program directly for your robotic kit, copy it, create new project in AVR Studio and then paste it into the coding area. Then compile it.

/*
	Level II project of 'Basics of Sensors'
	Applicable to ATMega8/16/32/128
	Designed by: Vidyasagar Academy, Akola
	Website: www.vsagar.org

	*** CONNECTION DETAILS OF KIT ***
	1) The 4 LEDs in your kit, are internally connected to PB4-PB1 
	2) Connect one IR sensor to PC0 in your kit.
*/

#define F_CPU 12000000UL // defining the crystal frequency 12MHz 
			// given on your dev. board of ATMega8

#include <avr/io.h> // including the input-output 
		// to define the input output ports and pins
		// this file is inside the AVR folder 					
	int main() // starting the main function of program

	{ // main function brace opened

	DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins

	DDRC=0b0000000;  // PC6-PC0 of PORTC are defined as input pins

	int s=0; // 's' is the variable to store the status value of sensor	
		// when we write 'int s', it creates a location in memory of 
		// microcontroller.
		// initially '0' is stored into 's' memory location

	while(1) // starting the infinite loop to repeat the action infinitely
	
	{ // while loop brace opened

	s=PINC&0b0000001; // assigning the variable 's' to PC0 of PORTC
			// so that the output status of sensor will be 
			// stored into the variable 's'
			// since one sensor in our kit is connected 
			// to PC0 and other to PC3 
			// Note: PC3 sensor is not used in this program
			// only PC0 sensor is used
	
	// Now we know that... 
	// When it is black surface below the sensor, its output=0
	// When it is white surface below it, its output=1

	// So when it is black below the sensor, all the LEDs should be OFF
	// When it is white below the sensor, all LEDs should be ON
	// This is achieved using following conditions -

		if(s==0b0000001) // white surface below the sensor
		{
		PORTB=0b00011110; // all LEDs will be ON
		}

		else // black surface below the sensor
		{
		PORTB=0b00000000; // all LEDs will be OFF
		}

	} // while loop brace closed

	} // main function brace closed

/* 
	=== HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT? ===
	1) First read the program carefully. Understand the steps as taught to you.
	2) Connect your kit to USB port.
	3) Burn the 'hex' file into your kit.
	4) Connect one IR sensor, to PC0 in your kit.
	5) Now connect battery and switch on the kit.
	6) Keep a white paper below the IR sensor.
	7) All the LEDs connected to PB4-PB1 will glow.
	8) Remove the white paper, so that all the LEDs will be OFF.
	9) Is it working? Nice! You did it.
	10) Now don't forget to give your feedback.
*/ 

If you liked this post please write Google feedback about us.
Thanks in advance!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.