Ultrasonic Sensor projects based onArduino
Development Board: Arduino Duemilanove
Microcontroller: ATmega 328
Ultrasonic Sensor: HC-SR 04
Guys, I happen to get involve myself in college Robotics Event and eventually start doing some hobbyist stuffs in my room. During the first year in my college, NIT Silchar I happen to do just minor manual robotics stuff like connecting wires and rotating motors using Doble Pole Double Throw (DPDT) switches, and rotating wheels and eventually making four wheeled robots and moving it in the path of desired path with the famous differential drive concepts. Well, representing the college in IIT Kharagpur was like a dream come true. The manual bot we designed was cool and we did great.
My second year in college I got involve in autonomous stuff, and i started digging into sensors and microcontrollers. I did a small summer training from a reputed company in Guwahati, India on embedded systems and learnt to work on pic microcontrollers. Coming back to college I started working in Arduino because when i saw some sample projects in the Arduino's official site arduino.cc, I found the codes a bit simpler than the embedded C I learnt in Guwahati.
So guys, this was the first project I did in college related to embedded system. I second year I was chosen the Co-ordinator of the Robotics Club of our college, and thus I happen to oganise the first ever Robotics Event in the college. So this small Ultrasonic sensor based module was also displayed in it.
The stuff is like, there is one ultrasonic sensor, which works on the principle of bat's navigation technique. The Ultrasonic sensor, HC-SR04 has two eyes. one is the trigger eye, which emits the ultrasonic sound of 40kHz. if an obstacle comes in the range of 2 cm to 450 cm, it is detected by the echo generated by it. The echo eye on recieving an echo generates a digital impulse. So we calculate the time elapse between the emittion of the sound to the receiving of the echo or more precisely the generation of digital impulse. The arduino codes are given below:
this code is designed so as to demonstrate the ultrasonic sensor and its uses. the code is deviced as of, the distances of the obstacle is measured by the LEDs glowing.
int trig = 1;
int echo = 2;
int ledRed = 4;
int ledGreen = 5;
int ledYellow = 6;
int ledBlue = 7;
void setup()
{
Serial.begin (9600);
pinMode(trig, OUTPUT); // Configuring digital pin 1 as output
pinMode(echo, INPUT); // Configuring digital pin 2 as input
pinMode(ledRed, OUTPUT); // Configuring digital pin 4 as output
pinMode(ledGreen, OUTPUT); // Configuring digital pin 5 as output
pinMode(ledYellow, OUTPUT); // Configuring digital pin 6 as output
pinMode(ledBlue, OUTPUT); // Configuring digital pin 7 as output
}
void loop()
{
int tym, cm;
digitalWrite(trig, LOW); // trigger remailns OFF for 2 microsecomds
delayMicroseconds(2);
digitalWrite(trig, HIGH); // trigger remains ON for 10 microseconds
delayMicroseconds(10);
digitalWrite(trig, LOW);
Serial.print(" "); // random delay
tym = pulseIn(echo, HIGH); // this is the time processor calculates since the time the last line of the code is executed
// this is basically the time elapsed between the transmission of Ultrasonic sound and recieving of its echo
cm = tym/2 / 29.1; // distance using the time
if(cm == 0)
{
Serial.print(cm); // If the obstacle is not detected, within a range of 4.5 meters, No echo is recieved.
delay(20); // Hence the time is taken to be NULL or )
digitalWrite(ledRed, LOW); // And hence the distance calculated is also NULL
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW); // this statement is a must.
digitalWrite(ledBlue, LOW); // here in this situation all the LEDS are in OFF state
}
if(cm > 0 && cm <= 10)
{
Serial.print( cm );
delay(20);
digitalWrite(ledRed, HIGH); // If the obstacle detected is in the range of 0 to 10 cm
digitalWrite(ledGreen, LOW); // Red LED glows
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, LOW);
}
if(cm > 10 && cm <= 20)
{
Serial.print( cm ); // If the object detected is in the range of 10 to 20 cm
delay(20); // Green LED glows
digitalWrite(ledRed, LOW);
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, LOW);
}
if(cm > 20 && cm <=30)
{
Serial.print( cm );
delay(20);
digitalWrite(ledRed, LOW); // if the obstacle is in the range of 20 to 30 cm
digitalWrite(ledGreen, LOW); // yellow LED glows
digitalWrite(ledYellow, HIGH);
digitalWrite(ledBlue, LOW);
}
if(cm > 30 && cm <= 40)
{
Serial.print ( cm );
delay(20); // If the obstacle is in the range of 30 to 40 cms
digitalWrite(ledRed, LOW); // Blue LED glows
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, HIGH);
}
delay(20);
}
this code is designed so as to demonstrate the ultrasonic sensor and its uses. the code is deviced as of, the distances of the obstacle is measured by the LEDs glowing.
int trig = 1;
int echo = 2;
int ledRed = 4;
int ledGreen = 5;
int ledYellow = 6;
int ledBlue = 7;
void setup()
{
Serial.begin (9600);
pinMode(trig, OUTPUT); // Configuring digital pin 1 as output
pinMode(echo, INPUT); // Configuring digital pin 2 as input
pinMode(ledRed, OUTPUT); // Configuring digital pin 4 as output
pinMode(ledGreen, OUTPUT); // Configuring digital pin 5 as output
pinMode(ledYellow, OUTPUT); // Configuring digital pin 6 as output
pinMode(ledBlue, OUTPUT); // Configuring digital pin 7 as output
}
void loop()
{
int tym, cm;
digitalWrite(trig, LOW); // trigger remailns OFF for 2 microsecomds
delayMicroseconds(2);
digitalWrite(trig, HIGH); // trigger remains ON for 10 microseconds
delayMicroseconds(10);
digitalWrite(trig, LOW);
Serial.print(" "); // random delay
tym = pulseIn(echo, HIGH); // this is the time processor calculates since the time the last line of the code is executed
// this is basically the time elapsed between the transmission of Ultrasonic sound and recieving of its echo
cm = tym/2 / 29.1; // distance using the time
if(cm == 0)
{
Serial.print(cm); // If the obstacle is not detected, within a range of 4.5 meters, No echo is recieved.
delay(20); // Hence the time is taken to be NULL or )
digitalWrite(ledRed, LOW); // And hence the distance calculated is also NULL
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW); // this statement is a must.
digitalWrite(ledBlue, LOW); // here in this situation all the LEDS are in OFF state
}
if(cm > 0 && cm <= 10)
{
Serial.print( cm );
delay(20);
digitalWrite(ledRed, HIGH); // If the obstacle detected is in the range of 0 to 10 cm
digitalWrite(ledGreen, LOW); // Red LED glows
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, LOW);
}
if(cm > 10 && cm <= 20)
{
Serial.print( cm ); // If the object detected is in the range of 10 to 20 cm
delay(20); // Green LED glows
digitalWrite(ledRed, LOW);
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, LOW);
}
if(cm > 20 && cm <=30)
{
Serial.print( cm );
delay(20);
digitalWrite(ledRed, LOW); // if the obstacle is in the range of 20 to 30 cm
digitalWrite(ledGreen, LOW); // yellow LED glows
digitalWrite(ledYellow, HIGH);
digitalWrite(ledBlue, LOW);
}
if(cm > 30 && cm <= 40)
{
Serial.print ( cm );
delay(20); // If the obstacle is in the range of 30 to 40 cms
digitalWrite(ledRed, LOW); // Blue LED glows
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledBlue, HIGH);
}
delay(20);
}