robotics practical file
Name : Ishika Gupta
Rollno:2022313
INDEX
S.NO QUESTIONS TEACHER’S
SIGNATURE
1. Interfacing experiment using available hardware like LCD,
LED, Buzzer, Motors.
2. Read IR proximity sensor to determine if there is some
object nearby and thus Control the motion of robot using
IR sensors
3. Simple Motion Control (programming the robot to move
forward, backward, left and right).
4. Line following Robot (programming the robot to move
along a define path, white line or black line).
5. Obstacle Detection (programming the robot for obstacle
detection, beeps the buzzer when detects the obstacle).
Q1. Interfacing experiment using available hardware like LCD, LED, Buzzer, Motors.
*LED/LCD BLINK
const int ledPin = LED_BUILTIN; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
*MOTORS
// Motor control pins
const int in1 = 8;
const int in2 = 9;
const int ena = 10; // PWM pin
void setup() {
// Set motor control pins as outputs
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ena, OUTPUT);
}
void loop() {
// Spin motor forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, 200); // Set speed (0-255)
delay(3000); // Run motor for 3 seconds
// Stop motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(ena, 0);
delay(3000); // Wait for 3 seconds
}
Q2 Read IR proximity sensor to determine if there is some object nearby and thus
Control the motion of robot using IR sensors.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h>
#include "lcd.c"
void port_init();
void timer5_init();
void velocity(unsigned char, unsigned char);
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
//Function to configure LCD port
void lcd_port_config (void)
{
DDRC = DDRC | 0xF7;
PORTC = PORTC & 0x80;
}
//ADC pin configuration
void adc_pin_config (void)
{
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
}
//Function to configure ports to enable robot's motion
void motion_pin_config (void)
{
DDRA = DDRA | 0x0F;
PORTA = PORTA & 0xF0;
DDRL = DDRL | 0x18;
PORTL = PORTL | 0x18;
}
//Function to Initialize PORTS
void port_init()
{
lcd_port_config();
adc_pin_config();
motion_pin_config();
}
// Timer 5 initialized in PWM mode for velocity control
void timer5_init()
{
TCCR5B = 0x00;
TCNT5H = 0xFF;
TCNT5L = 0x01;
OCR5AH = 0x00;
OCR5AL = 0xFF;
OCR5BH = 0x00;
OCR5BL = 0xFF;
OCR5CH = 0x00;
OCR5CL = 0xFF;
TCCR5A = 0xA9;
TCCR5B = 0x0B;
}
//Function for calculating distance in mm from Sharp IR sensor
unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading)
{
float distance;
unsigned int distanceInt;
distance = (int)(10.00 * (2799.6 * (1.00 / (pow(adc_reading, 1.1546)))));
distanceInt = (int)distance;
if(distanceInt > 800)
{
distanceInt = 800;
}
return distanceInt;
}
void adc_init()
{
ADCSRA = 0x00;
ADCSRB = 0x00;
ADMUX = 0x20;
ACSR = 0x80;
ADCSRA = 0x86;
}
//Function For ADC Conversion
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
if(Ch>7)
{
ADCSRB = 0x08;
}
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40;
while((ADCSRA&0x10)==0);
a=ADCH;
ADCSRA = ADCSRA|0x10;
ADCSRB = 0x00;
return a;
}
//Function for velocity control
void velocity (unsigned char left_motor, unsigned char right_motor)
{
OCR5AL = (unsigned char)left_motor;
OCR5BL = (unsigned char)right_motor;
}
//Function used for setting motor's direction
void motion_set (unsigned char Direction)
{
unsigned char PortARestore = 0;
Direction &= 0x0F;
PortARestore = PORTA;
PortARestore &= 0xF0;
PortARestore |= Direction;
PORTA = PortARestore;
}
void forward (void)
{
motion_set (0x06);
}
void stop (void)
{
motion_set (0x00);
}
void init_devices (void)
{
cli();
port_init();
adc_init();
timer5_init();
sei();
}
int main()
{
init_devices();
lcd_set_4bit();
lcd_init();
unsigned int distance;
unsigned char sharp_reading;
while(1)
{
sharp_reading = ADC_Conversion(11);
distance = Sharp_GP2D12_estimation(sharp_reading);
lcd_cursor(2, 1);
lcd_string("Dist:");
lcd_print(2, 7, distance, 3);
if (distance < 200)
{
stop();
velocity(0, 0);
}
else
{
forward();
velocity(150, 150);
}
}
}
Q4 Simple Motion Control(programming the robot to move forward, backward,
left and right).
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
void motion_pin_config (void)
{
DDRA = DDRA | 0x0F; //set direction of the PORTA 3 to PORTA 0 pins as output
PORTA = PORTA & 0xF0; // set initial value of the PORTA 3 to PORTA 0 pins to logic 0
DDRL = DDRL | 0x18; //Setting PL3 and PL4 pins as output for PWM generation
PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control using PWM
}
//Function to initialize ports
void port_init()
{
motion_pin_config();
}
//Function used for setting motor's direction
void motion_set (unsigned char Direction)
{
unsigned char PortARestore = 0;
Direction &= 0x0F; // removing upper nibbel as it is not needed
PortARestore = PORTA; // reading the PORTA's original status
PortARestore &= 0xF0; // setting lower direction nibbel to 0
PortARestore |= Direction; // adding lower nibbel for direction command and restoring the
PORTA status
PORTA = PortARestore; // setting the command to the port
}
void forward (void) //both wheels forward
{
motion_set(0x06);
}
void back (void) //both wheels backward
{
motion_set(0x09);
}
void left (void) //Left wheel backward, Right wheel forward
{
motion_set(0x05);
}
void right (void) //Left wheel forward, Right wheel backward
{
motion_set(0x0A);
}
void soft_left (void) //Left wheel stationary, Right wheel forward
{
motion_set(0x04);
}
void soft_right (void) //Left wheel forward, Right wheel is stationary
{
motion_set(0x02);
}
void soft_left_2 (void) //Left wheel backward, right wheel stationary
{
motion_set(0x01);
}
void soft_right_2 (void) //Left wheel stationary, Right wheel backward
{
motion_set(0x08);
}
void stop (void) //hard stop
{
motion_set(0x00);
}
void init_devices (void)
{
cli(); //Clears the global interrupts
port_init();
sei(); //Enables the global interrupts
}
//Main Function
int main()
{
init_devices();
while(1)
{
forward(); //both wheels forward
_delay_ms(1000);
stop();
_delay_ms(500);
back(); //bpth wheels backward
_delay_ms(1000);
stop();
_delay_ms(500);
left(); //Left wheel backward, Right wheel forward
_delay_ms(1000);
stop();
_delay_ms(500);
right(); //Left wheel forward, Right wheel backward
_delay_ms(1000);
stop();
_delay_ms(500);
soft_left(); //Left wheel stationary, Right wheel forward
_delay_ms(1000);
stop();
_delay_ms(500);
soft_right(); //Left wheel forward, Right wheel is stationary
_delay_ms(1000);
stop();
_delay_ms(500);
soft_left_2(); //Left wheel backward, right wheel stationary
_delay_ms(1000);
stop();
_delay_ms(500);
soft_right_2(); //Left wheel stationary, Right wheel backward
_delay_ms(1000);
stop();
_delay_ms(1000);
}
}
Q5 Line following Robot ( programming the robot to move along a define path,
white line or black line).
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h> //included to support power function
#include "lcd.c"
void port_init();
void timer5_init();
void velocity(unsigned char, unsigned char);
void motors_delay();
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
unsigned char flag = 0;
unsigned char Left_white_line = 0;
unsigned char Center_white_line = 0;
unsigned char Right_white_line = 0;
//Function to configure LCD port
void lcd_port_config (void)
{
DDRC = DDRC | 0xF7; //all the LCD pin's direction set as output
PORTC = PORTC & 0x80; // all the LCD pins are set to logic 0 except PORTC 7
}
//ADC pin configuration
void adc_pin_config (void)
{
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
}
//Function to configure ports to enable robot's motion
void motion_pin_config (void)
{
DDRA = DDRA | 0x0F;
PORTA = PORTA & 0xF0;
DDRL = DDRL | 0x18; //Setting PL3 and PL4 pins as output for PWM generation
PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control using PWM.
}
//Function to Initialize PORTS
void port_init()
{
lcd_port_config();
adc_pin_config();
motion_pin_config();
}
// Timer 5 initialized in PWM mode for velocity control
// Prescale:256
// PWM 8bit fast, TOP=0x00FF
// Timer Frequency:225.000Hz
void timer5_init()
{
TCCR5B = 0x00; //Stop
TCNT5H = 0xFF; //Counter higher 8-bit value to which OCR5xH value is compared with
TCNT5L = 0x01; //Counter lower 8-bit value to which OCR5xH value is compared with
OCR5AH = 0x00; //Output compare register high value for Left Motor
OCR5AL = 0xFF; //Output compare register low value for Left Motor
OCR5BH = 0x00; //Output compare register high value for Right Motor
OCR5BL = 0xFF; //Output compare register low value for Right Motor
OCR5CH = 0x00; //Output compare register high value for Motor C1
OCR5CL = 0xFF; //Output compare register low value for Motor C1
TCCR5A = 0xA9; /*{COM5A1=1, COM5A0=0; COM5B1=1, COM5B0=0; COM5C1=1 COM5C0=0}
For Overriding normal port functionality to OCRnA outputs.
{WGM51=0, WGM50=1} Along With WGM52 in TCCR5B for Selecting FAST PWM 8-bit Mode*/
TCCR5B = 0x0B; //WGM12=1; CS12=0, CS11=1, CS10=1 (Prescaler=64)
}
//Function for calculating distance in mm from Sharp IR sensor
unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading)
{
float distance;
unsigned int distanceInt;
distance = (int)(10.00 * (2799.6 * (1.00 / (pow(adc_reading, 1.1546)))));
distanceInt = (int)distance;
if(distanceInt > 800)
{
distanceInt = 800;
}
return distanceInt;
}
void adc_init()
{
ADCSRA = 0x00;
ADCSRB = 0x00; //MUX5 = 0
ADMUX = 0x20; //Vref=5V external --- ADLAR=1 --- MUX4:0 = 0000
ACSR = 0x80;
ADCSRA = 0x86; //ADEN=1 --- ADIE=1 --- ADPS2:0 = 1 1 0
}
//Function For ADC Conversion
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
if(Ch>7)
{
ADCSRB = 0x08;
}
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40; //Set start conversion bit
while((ADCSRA&0x10)==0); //Wait for conversion to complete
a=ADCH;
ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
ADCSRB = 0x00;
return a;
}
//Function To Print Sesor Values At Desired Row And Coloumn Location on LCD
void print_sensor(char row, char coloumn,unsigned char channel)
{
ADC_Value = ADC_Conversion(channel);
lcd_print(row, coloumn, ADC_Value, 3);
}
//Function for velocity control
void velocity (unsigned char left_motor, unsigned char right_motor)
{
OCR5AL = (unsigned char)left_motor;
OCR5BL = (unsigned char)right_motor;
}
//Function used for setting motor's direction
void motion_set (unsigned char Direction)
{
unsigned char PortARestore = 0;
Direction &= 0x0F; // removing upper nibbel for the protection
PortARestore = PORTA; // reading the PORTA original status
PortARestore &= 0xF0; // making lower direction nibbel to 0
PortARestore |= Direction; // adding lower nibbel for forward command and restoring the PORTA status
PORTA = PortARestore; // executing the command
}
void forward (void)
{
motion_set (0x06);
}
void stop (void)
{
motion_set (0x00);
}
void init_devices (void)
{
cli(); //Clears the global interrupts
port_init();
adc_init();
timer5_init();
sei(); //Enables the global interrupts
}
int main()
{
init_devices();
lcd_set_4bit();
lcd_init();
unsigned int distance;
unsigned char sharp_reading;
while(1)
{
// Read distance from Sharp sensor at ADC channel 11 (PK3)
sharp_reading = ADC_Conversion(11);
distance = Sharp_GP2D12_estimation(sharp_reading);
// Display distance on LCD
lcd_cursor(2, 1);
lcd_string("Dist:");
lcd_print(2, 7, distance, 3); // Show distance in mm
// Stop if obstacle is closer than 200mm
if (distance < 200)
{
stop();
velocity(0, 0);
continue; // Skip rest of the line following logic
}
// Line follower logic
Left_white_line = ADC_Conversion(3);
Center_white_line = ADC_Conversion(2);
Right_white_line = ADC_Conversion(1);
flag = 0;
print_sensor(1, 1, 3); // Left
print_sensor(1, 5, 2); // Center
print_sensor(1, 9, 1); // Right
if(Center_white_line < 0x28)
{
flag = 1;
forward();
velocity(150,150);
}
if((Left_white_line > 0x28) && (flag == 0))
{
flag = 1;
forward();
velocity(130,50);
}
if((Right_white_line > 0x28) && (flag == 0))
{
flag = 1;
forward();
velocity(50,130);
}
if(Center_white_line > 0x28 && Left_white_line > 0x28 && Right_white_line > 0x28)
{
forward();
velocity(0,0);
}
}
}
Q6 Obstacle Detection (programming the robot for obstacle detection , beeps the
buzzer when detects the obstacle).
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h> // For power function
#include "lcd.c"
unsigned char ADC_Conversion(unsigned char);
unsigned char ADC_Value;
unsigned char sharp, distance, adc_reading;
unsigned int value;
float BATT_Voltage, BATT_V;
// LCD pin configuration
void lcd_port_config(void)
{
DDRC = DDRC | 0xF7; // All LCD pins as output
PORTC = PORTC & 0x80;
}
// ADC pin configuration
void adc_pin_config(void)
{
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
}
// Buzzer pin configuration (PC3)
void buzzer_pin_config(void)
{
DDRC = DDRC | 0x08; // PC3 as output
PORTC = PORTC & 0xF7; // Buzzer off initially
}
// Turn buzzer ON
void buzzer_on(void)
{
PORTC = PORTC | 0x08; // Set PC3 high
}
// Turn buzzer OFF
void buzzer_off(void)
{
PORTC = PORTC & 0xF7; // Clear PC3
}
// Port initialization
void port_init(void)
{
lcd_port_config();
adc_pin_config();
buzzer_pin_config();
}
// ADC initialization
void adc_init(void)
{
ADCSRA = 0x00;
ADCSRB = 0x00;
ADMUX = 0x20;
ACSR = 0x80;
ADCSRA = 0x86;
}
// ADC Conversion function
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
if(Ch > 7)
{
ADCSRB = 0x08;
}
Ch = Ch & 0x07;
ADMUX = 0x20 | Ch;
ADCSRA |= 0x40;
while((ADCSRA & 0x10) == 0);
a = ADCH;
ADCSRA |= 0x10; // Clear ADIF
ADCSRB = 0x00;
return a;
}
// Print sensor value to LCD
void print_sensor(char row, char coloumn, unsigned char channel)
{
ADC_Value = ADC_Conversion(channel);
lcd_print(row, coloumn, ADC_Value, 3);
}
// Calculate distance in mm from Sharp reading
unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading)
{
float distance;
unsigned int distanceInt;
distance = (int)(10.00 * (2799.6 * (1.00 / (pow(adc_reading,
1.1546)))));
distanceInt = (int)distance;
if(distanceInt > 800)
{
distanceInt = 800;
}
return distanceInt;
}
// Device initialization
void init_devices(void)
{
cli(); // Disable global interrupts
port_init();
adc_init();
sei(); // Enable global interrupts
}
// Main function
int main(void)
{
unsigned int value;
init_devices();
lcd_set_4bit();
lcd_init();
while(1)
{
sharp = ADC_Conversion(11); // Front sharp sensor on ADC11
value = Sharp_GP2D12_estimation(sharp);
lcd_cursor(1,1);
lcd_string("Dist:");
lcd_print(2, 14, value, 3); // Print distance on LCD
if(value < 200)
{
buzzer_on(); // Beep when object is close
}
else
{
buzzer_off(); // Silence otherwise
}
_delay_ms(100); // Small delay to avoid flicker
}
}