Today we’ll take a look at how you can put together a basic Bluetooth controlled 2 wheel drive Arduino robot.
Click here to download the Android app.
Check us out on Facebook!
Parts List:
#affiliate links#
Battery
Schematic:
ARDUINO Code:
#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 –
#define pw1 9 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 5 // motor #2 –
#define pw2 6 // motor #2 pwm
void setup() {
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pw1, OUTPUT);
pinMode(pw2, OUTPUT);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
analogWrite(pw1, 0);
analogWrite(pw2, 0);
}
void loop() {
if (Serial.available() >= 2 )
{
unsigned int a = Serial.read();
unsigned int b = Serial.read();
unsigned int val = (b * 256) + a;
if (val == 1) //forward
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
analogWrite(pw1,255);
analogWrite(pw2,255);
}
else if (val == 2) //reverse
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
analogWrite(pw1,255);
analogWrite(pw2,255);
}
else if (val == 3) //stop
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
analogWrite(pw1,0);
analogWrite(pw2,0);
}
else if (val == 5) //right
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
analogWrite(pw1,125);
analogWrite(pw2,125);
}
else if (val == 4) //lefl
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
analogWrite(pw1,125);
analogWrite(pw2,125);
}
}
}