For object avoidance, the ultrasonic or the ir range finders do not always see and object and i often notice my robot running into things and getting stuck. I figured that using bumper sensors would reduce the chances of getting stuck by a fair amount. Well to build a bumper sensor we are going to need:
1. Limit switches. I bought mine from Jaycar for 1.25 each. On ebay you can get 10 for $5.75 from Hongkong.
2. some resistors mine are 2200ohm
3. some cable
4. soldering iron
5. solder
6. some patience.
My limit switches are rated at 250v 5a. They were the only ones available to me at that time aso i purchased these.
a 2k2 ohm resistor (2200 ohm)
Some wires. You need 3 in total.
And our Schematic. (mine work well with a 2200 ohm resistor.
or this:
How is it all connected?
Ground goes to the ground pin, The VCC or 5V supply is connected toone end of the 2200ohm resistor which is connected to the NORMALLY OPEN tab of the limit switch and the output cable for goes from the normally open tab to the arduino to be read by the arduino as HIGH or LOW. I hope the image below explains it a lot better.
Here is some arduino code to test it out. I will be using the Arduino Mega R3 and Elechouse 50A motor controller for this.
The motor will run for3 seconds only if the switch has been hit and will stop. Pressing the swtich again will run the motor again and so on.
//limit switch testcode by Tawanda
//I am using the Arduino Mega 2560 R3
int limswitch =23;// define limit switch and allocate its pin
int ENA=51;//Enable for motor controller at pin 51
int RPWMA =45;//RPWM
int LPWMA =43;//Left pwm
int led= 13;//default arduino led.
void setup()
{
pinMode(ENA, OUTPUT);
pinMode(RPWMA,OUTPUT);
pinMode(LPWMA,OUTPUT);
pinMode(led,OUTPUT);
pinMode(limswitch, INPUT); // set your switch pin as an input so that it can be read by the arduino
}
void loop() //The motor will run for 3 seconds and then turn off until you hit the switch again
{
int switch1 = digitalRead(limswitch); //define the limit switch as switch 1
if (switch1 == HIGH) //if the pin reads 5v
{
//run motor for 3 seconds
digitalWrite(ENA,HIGH);
analogWrite(RPWMA,50);
digitalWrite(LPWMA,HIGH);
delay(3000);
//SHUT OFF MOTOR
digitalWrite(ENA,HIGH);
analogWrite(RPWMA,LOW);
digitalWrite(LPWMA,LOW);
}
}