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?
- 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)
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.
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
pinMode(RF, OUTPUT); //RF
pinMode(RR, OUTPUT); //RR
pinMode(LF, OUTPUT); //LF
pinMode(LR, OUTPUT); //LR
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.