Our first project: A car of our own (Part 3 Manual Control)

One day, it would be good to let it do full self driving. For now, we’ll be working with manual control where your mobile phone is the controller.

Quick rundown of serial communication: It’s when you send data (communicate) to things using serial. USB (Universal Serial Bus) has become the most common lately, the different heads are just to connect to things of different sizes.

Parts:

  • The ‘tank’ previously made
  • Bluetooth module
  • Android phone with Bluetooth -> Arduino app installed

I made a (very simple) Android app that allows you to send data to Arduino via Bluetooth. Technically you can send data to anything that uses Bluetooth (Raspberry Pi for example) but the focus is with Arduino. I’ve released it 100% free, no ads and open source.

I’m going to be working on it and improving it over time, however you can find it on the Google Play Store here.

Assembly:

Another diagram!

Very similar, only addition is the Bluetooth module. To make it obvious how to wire everything up, here’s what you do:

  • VCC -> 5v
  • GND -> GND
  • RXD -> TXD
  • TXD -> RXD

Unless you are using an Arduino Leonardo or another that has multiple serial ports (if you don’t know for sure, assume you only have one serial port) you will need to disconnect and unplug the Bluetooth module completely whenever you are uploading new code to the Arduino.

Code:

Here is the code you will want on your Arduino:

byte serialA;
int RF = 11;
int RR = 10;
int LF = 9;
int LR = 6;
void setup()
{
Serial.begin(9600);
pinMode(11, OUTPUT); //RF
pinMode(10, OUTPUT); //RR
pinMode(9, OUTPUT); //LF
pinMode(6, OUTPUT); //LR
}

void loop()
{
if (Serial.available() > 0) {serialA = Serial.read();Serial.println(serialA);}

switch (serialA) {
case 49: //Forward on text 1 through serial
digitalWrite(RF, HIGH);
digitalWrite(LF, HIGH);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 50: //LEFT on text 2 through serial
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
break;
case 51: //RIGHT on text 3 through serial
digitalWrite(LF, HIGH);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, HIGH);
break;
case 52: //Reverse on text 4 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RR, HIGH);
break;
}
Serial.print("Received the following: ");
Serial.println(serialA);
}

The switch looks at serialA and depending on what it is will do different cases. The reason for case 49 is because ASCII 1 gets translated to decimal 49 while being sent/received (I’m surprised it isn’t translated to 39 which is 1 in hex but whatever)

Anyway, by now you have your code uploaded, you have the app installed on your phone. Plug the Bluetooth module in and the little red light should start blinking on it. That shows it’s ready to be paired and connected.

Pair it with your phone (the pin should be 1234. If it isn’t, try 0000. If it still isn’t working, double check everything) and then open the app, find the Bluetooth module and then send data to it. Best to send 1 and watch your tank/car/whatever take off.

Our first project: A car of our own (Part 2 Movement)

A sneak peak at how this stage will finish:

Don’t worry, it’s spinning in place on purpose. It’s just easier to show off this way. You’ll understand why when you see the code later. This part will focus on uploading code and digital output.

Speaking of code… If you don’t already have the Arduino IDE installed, download and install it now.

Ok, so in our previous part, we did a bunch of planning, this time we’ll do building. What will you need?

Parts:

  • Arduino (Micro, Mini, Uno, doesn’t matter. I used a Micro)
  • H-Bridge IC chip (SN754410NE is what I used. There is others. It’s possible to use a shield, but using a single IC means it’s smaller over all)
  • Batteries (6v total, I used four 1.5v batteries)
  • Two motors (Geared or not, your choice. Mine are geared)
  • 5 volt regulator (Turns higher voltages down to 5 volts)
  • Body and wheels (This is all personal opinion. I’ve got a tank. So many options)

Assembly:

Time for a circuit diagram!

I made that with Fritzing which is a great tool.

Now let’s go through this! If you want to know how an H-Bridge controls the motors and why it’s needed, there are plenty of videos on it (in fact I probably should make one… But I’m a bit lazy) but basically wire up everything as shown there. The Arduino will control how the H-Bridge controls the motors which will control how the tank moves.

Code:

This is VERY simple code. This is simply to make it spin in place to make sure everything is working.

int RF = 11; //Right Forward pin
int RR = 10; //Right Reverse pin
int LF = 9; //Left Forward pin
int LR = 6; //Left Reverse pin

void setup()
{
pinMode(RF, OUTPUT); //RF
pinMode(RR, OUTPUT); //RR
pinMode(LF, OUTPUT); //LF
pinMode(LR, OUTPUT); //LR
}

void loop(){
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
delay(500);
}

Going through this bit by bit this is what we get:

First we define which pins are doing what. For example, pin 11 is tasked with making the right motor go ‘forward’  when it is on (high) and pin 10 (Right Reverse) is off (low).

In setup, we do just that. Set everything up. We tell the Arduino what each of the pins are doing. In this case all the pins are going to be OUTPUT pins. IE: they can turn on and off, either giving 0 or 5 volts. This only runs once whenever the Arduino boots up.

Then loop. This will loop forever. As long as the Arduino has power it will loop. So here, it will tell the pins “Pin 9, go low. Pin 6, go high. Pin 11, go high. Pin 10, go low.” It will then wait 500 milliseconds, then it will repeat the process.

Next up: More! Something… I don’t know the kids just woke up.