t r o n i x s t u f f

fun and learning with electronics

Getting Started with Arduino! – Chapter Five

This is part of a series titled “Getting Started with Arduino!” – A tutorial on the Arduino microcontrollers. The first chapter is here, and the complete index here.

Welcome back fellow arduidans!

Hello once again to our weekly Arduino instalment. This week are up to all sorts of things, including: more shiftiness with shift registers, more maths, 7-segment displays, arduinise a remote control car, and finally make our own electronic game! Wow – let’s get cracking…

In the last chapter we started using a 74HC595 shift register to control eight output pins with only three pins on the Arduino. That was all very interesting and useful – but there is more! You can link two or more shift registers together to control more pins! How? First of all, there is pin we haven’t looked at yet – pin 9 on the ’595. This is “data out”. If we connect this to the data in pin (14) on another ’595, the the first shift register can shift a byte of data to the next shift register, and so on.

Recall from our exercise 4.1, this part of the sketch:

digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, loopy);
digitalWrite(latchpin, HIGH);

If we add another shiftOut(); command after the first one, we are sending two bytes of data to the registers. In this situation the first byte is accepted by the first shift register (the one with its data in pin [14] connected to the Arduino), and when the next byte is sent down the data line, it “pushes” the byte in the first shift register into the second shift register, leaving the second byte in the first shift register.

So now we are controlling SIXTEEN output pins with only three Arduino output pins. And yes – you can have a third, fourth … if anyone sends me a link to a Youtube clip showing this in action with 8 x 74HC595s, I will send them a prize. So, how do we do it? The code is easy, here is the sketch.

On the hardware side, it is also quite simple. If you love blinking LEDs this will make your day. It is the same as exercise 4.1, but you have another 74HC595 connected to another 8 LEDS. The clock and latch pins of both ’595s are linked together, and there is a link between pin 9 of the first register and pin 14 of the second. Below is a photo of my layout:

and a video:

Can you think of anything that has seven or eight LEDs? Hopefully this photo will refresh your memory:

Quickie – if you want to find out the remainder from a quotient, use modulo – “%”. For example:

a = 10 % 3;

returns a value of 1; as 10 divided by 3 is 3 remainder 1.

and

If you need to convert a floating-point number to an integer, it is easy. Use int();. It does not round up or down, only removes the fraction and leaves the integer.

Anyhow, now we can consider controlling these numeric displays with our arduino via the 74HC595. It is tempting to always use an LCD, but if you only need to display a few digits, or need a very high visibility, LED displays are the best option. Futhermore, they use a lot less current than a backlit LCD, and can be read from quite a distance away. A 7-segment display consists of eight LEDs arrange to form the digit eight, with a decimal point. Here is an example pinout digram:

Note that pinouts can vary, always get the data sheet if possible.

Displays can either be conmmon-anode, or common-cathode. That is, either all the LED segment anodes are common, or all the cathodes are common. Normally we will use common-cathode, as we are “sourcing” current from our shift register through a resistor (560 ohm), through the LED then to ground. If you use a common-anode, you need to “sink” current from +5v, through the resistor and LED, then into the controller IC. Now you can imagine how to display digits using this type of display – we just need to shiftout(); a byte to our shift register that is equavalent to the binary representation of the number you want to display.

Huh?

Let’s say we want to display the number ’8′ on the display. You will need to light up all the pins except for the decimal point. Unfortunately not all 7-segment displays are the same, so you need to work out which pinout is for each segment (see your data sheet) and then find the appropriate binary number to represent the pins needed, then convert that to a base-10 number to send to the display. I have created a table to make this easier:

And here is a blank one for you to print out and use: blank pin table.pdf.

Now let’s wire up one 7-segment display to our Arduino and see it work. Instead of the eight LEDs used in exercise 4.1 there is the display module. For reference the pinouts for my module were (7,6,4,2,1,9,10,5,3,8) = (a,b,c,d,e,f,g,DP, C, C) where DP is the decimal point and C is a cathode (which goes to GND). The sketch: example 5.2. Note in the sketch that the decimal point is also used; it’s byte value in this example is 128. If you add 128 to the value of loopy[] in the sketch, the decimal point will be used with the numbers.

and the video:

There you go – easily done. Now it is time for you to do some work!

Exercise 5.1

Produce a circuit to count from 0 to 99 and back, using two displays and shift-registers. It isn’t that difficult, the hardware is basically the same as example 5.1 but using 7-segment displays.

You will need:

  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • Two 7-segment, common-cathode displays
  • Two 74HC595 shift registers
  • 16 x 560 ohm 0.25 W resistors. For use as current limiters between the LED display segments and ground
  • a breadboard and some connecting wire
  • some water

You are probably wondering how on earth to separate the digits once the count hits 10… a hint: 34 modulo 10 = 4. 34 divided by 10 = 3.4 … but 3.4 isn’t an integer. While you are thinking, here is the shot of my layout:

and the ubiquitous video:

And here is the sketch for my interpretation of the answer.

I hope you have gained more of an understanding of the possibilities available with shift registers. We will contiunue with more next week.
However, next on our agenda is some real-world hacking. This section of the chapter is more of a commentary than the usual format, but I hope you find it interesting and you receive some inspiration or ideas after reading it.

Although we have been having fun (well I have been, hopefully someone else is as well) making things on our desks or work benches, it is time to slowly enter the real world and hack something up. The other day I was in a variety store to buy some glue, and happened across a very cheap remote-control car. After noticing it had full directional control (left/right, forwards/backwards) it occured to me that we could control it with an arduino. So $9 later here it is on my desk:

Naturally I stopped everything else and had a play with it. But by crikey it was very fast:

The first thing to do would be slow this baby down. Due to the …cheapness of the product it did not have variable speed control. So the first thing to do was pull the body off to see what we had to work with:

The design is very simple, with one motor controlling the steering, and one for the speed. Luckily for me there were four wires heading to the motor from the PCB, and the were very easy to get to.

Normally we could use pulse-width modulation to slow motors down, but I don’t think we could send a PWM signal over radio control. Instead, it would be easier to reduce the voltage going to the drive motor in order to slow it down. So with the car up on blocks, the motor was set to forward with the remote and I measure the voltages across the four wires. Black and green was +3.7 in forwards, nothing in reverse, black and red was the same in reverse, and nothing forwards. Easy – just find out how much current the motor draws at full speed and then we can use Ohm’s law (voltage = current x resistance) to calculate the value of a resistor to slow it down about 70% or so.

The motor initially drew ~500 mA to start up and then reduced to ~250 mA once it got going after around one second. However, a various range of resistors from 10 to 120 ohm didn’t really seem to have much effect, and a 560 ohm knocked it out all together. So instead of trying to control speed with a hardware method, we will try with a software method… perhaps try PWM after all, or create our own.

But now, time to get the arduino interfaced with the transmitter unit. Firstly I reassembled the car, then started on the transmitter:

After cutting my finger trying to get the transmitted open, it eventually gave in and cracked open. But the effort was worth it – the control buttons were very simple rubber pads over the PCB spots:

Excellent – each controller was basically a SPDT switch, and there is plenty of space on the PCB to solder in some wires to run to the Arduino and a breadboard. The push buttons could be replaced with BC548 transistors controlled by our Arduino – the same we we controlled a relay in Chapter Three.

Next was to solder some wires from the PCB that could run to my breadboard:

The green wire is a common return, and the yellow wires are forwards, reverse, left and right. Now to set up the breadboard. Four digital out pins, connected to the base of a BC548 transistor via a 1k resistor. The emitters are connected to GND, which is also connected to the GND of the transmitter.

Just as I had finished making up the breadboard, after turning around to close a window my arm brushed the transmitter and it made a ‘crack’ noise.

My soldering had come unstuck. Oh well, it was only reverse! Time to get moving anyhow. Once again, I put the car up on blocks and uploaded the following sketch: (download)
/*
Example 5.3
Control a toy remote control car with arduino
Chapter Five @ http://www.tronixstuff.com/tutorials
*/
int forward = 12;
int left = 9;
int right = 7;
int del = 5000;
void setup()
{
pinMode(forward, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
}
void loop()
{
digitalWrite(right, HIGH);
delay(1000);
digitalWrite(right, LOW);
delay(1000);
digitalWrite(left, HIGH);
delay(1000);
digitalWrite(left, LOW);
delay(1000);
digitalWrite(forward, HIGH);
delay(del);
digitalWrite(forward, LOW);
delay(1000);
}

It cycles throgh the three (working!) function of the car. Let’s see what happens:

That’s a good start, things are moving when we want them to move. However the car’s motors seem to be pulsing. Perhaps the resistor-transistor bridge to the arduino had something to do with that. So I threw caution to the wind and connected the digital output pins directly to the transmitter. Wow! That fixed it. The motors are going at full speed now

Using our knowledge of Arduino sketches it will be east to make this car to drive around. Let’s try that now… here is our sketch:

/*
 Example 5.4
Control a toy remote control car with arduino - figure eight
Chapter Five @ http://www.tronixstuff.com/tutorials
*/
int forward = 12;
int left = 9;
int right = 7;
int del = 5000;
void setup()
{
pinMode(forward, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
}
// to make creating the car's journey easier, here are some functions
void goleft(int runtime)
{
digitalWrite(left, HIGH);  // tell the steering to turn left
digitalWrite(forward, HIGH); // move the car forward
delay(runtime);
digitalWrite(forward, LOW);
digitalWrite(left, LOW);  // tell the steering to straighen up
}
void goright(int runtime)
{
digitalWrite(right, HIGH);  // tell the steering to turn right
digitalWrite(forward, HIGH); // move the car forward
delay(runtime);
digitalWrite(forward, LOW);
digitalWrite(right, LOW);  // tell the steering to straighen up
}
void goforward(int runtime)
// run the drive motor for "runtime" milliseconds
{
digitalWrite(forward, HIGH);  // start the drive motor forwards
delay(runtime);
digitalWrite(forward, LOW); // stop the drive motor
}
void loop()
{
goforward(1000);
goleft(1000);
goright(1000);
}

For some reason now forwards made the car go backwards. And only when I removed the GND wire from the Arduino to the breadboard. Interesting, but perhaps a problem for another day.

There we have it. Our first attempt at taking over something from the outside world and arduinising it. Now it is back to our normal readings with an exercise!

Exercise 5.2

Once again it is your turn to create something. We have discussed binary numbers, shift registers, analogue and digital inputs and outputs, creating our own functions, how to use various displays, and much more. So our task now is to build a binary quiz game. This is a device that will:

  • display a number between 0 and 255 in binary (using 8 LEDs)
  • you will turn a potentiometer (variable resistor) to select a number between 0 and 255, and this number is displayed using three 7-segment displays
  • You then press a button to lock in your answer. The game will tell you if you are correct or incorrect
  • Basically a “Binary quiz” machine of some sort!

I realise this could be a lot easier using an LCD, but that is not part of the exercise. Try and use some imagination with regards to the user interface and the difficulty of the game. At first it does sound difficult, but can be done if you think about it. At first you should make a plan, or algorithm, of how it should behave. Just write in concise instructions what you want it to do and when. Then try and break your plan down into tasks that you can offload into their own functions. Some functions may even be broken down into small functions – there is nothing wrong with that – it helps with planning and keeps everything neat and tidy. You may even find yourself writing a few test sketches, to see how a sensor works and how to integrate it into your main sketch. Then put it all together and see!

You will need: (to recreate my example below)

  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • Three 7-segment, common-cathode displays
  • eight LEDs (for binary number display)
  • Four 74HC595 shift registers
  • 32 x 560 ohm 0.25 W resistors. For use as current limiters between the LED display segments and ground
  • a breadboard and some connecting wire
  • 10k linear potentiometer (variable resistor)
  • some water

For inspiration here is a photo of my layout:


and a video of the game in operation. Upon turning on the power, the game says hello. You press the button to start the game. It will show a number in binary using the LEDs, and you select the base-10 equivalent using the potentiometer as a dial. When you select your answer, press the button  - the quiz will tell you if you are correct and show your score; or if you are incorrect, it will show you the right answer and then your score.

I have set it to only ask a few questions per game for the sake of the demonstration:

And yes – here is the sketch for my answer to the exercise.

Now this chapter is over. Hope you had fun! Now move on to Chapter Six.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

May 6, 2010 - Posted by | 74hc595, arduino, education, microcontrollers, tutorial | , , , , , , , , , , , , , , , ,

17 Comments »

  1. Hi..
    i have a question about the 74HC595. How can you read inputs plugged in it? i want to plug a matrix 4×4 keypad…
    thank you for your answer…..

    Comment by Mr Guy | June 24, 2011 | Reply

  2. John, Thanks for the helpful tutorial. On the car sketch, you have void go–(int runtime) but runtime is never given a value. Is this a misprint, or how does this work?

    Thanks, Russ

    Comment by Russ | October 6, 2011 | Reply

    • The value for runtime is entered when using the function. See void loop(). When we use (for example)
      goforward(1000);
      runtime is given a value of 1000. Etc.
      cheers
      john

      Comment by John Boxall | October 6, 2011 | Reply

  3. Interesting. Just a question though. As the result of filling out the 7 segment table will always be:
    {63,6,91,79,102,109,125,7,127,111 } for the base-10 values,am i correct to presume that the actual decision on what segment lights up is made by selecting which segment pin is connected to which 595 pin?
    But can I presume that regardless of the pin-out of the 7 segment display, it will always be:
    595 pin Q0 -> a
    595 pin Q1-> b
    ….
    595 pin Q6->g
    595 pin Q7-> DP ?

    Also, when using 2 x 595 for 2 x 7segment displays, do I understand correctly that the 2nd display will be fed from the 2nd 595 and not partly from the remaining pins on the first 595?

    And lastly, it seems to me that the base-10 values from the table are solely for common cathode displays and that for common anode displays, the value shld be the binary inverse after all, they are hanging from the + line and have the segment -via a resister- coupled to the 595. That particular pin shld be low to light up the segment, whereas it wld need to be high to light up a common cathode segment
    Am I correct?

    Comment by ed | December 22, 2011 | Reply

    • You need to confirm the pinouts of your 7-segment display, however in my tutorials – yes Q0>display ‘a’, etc. Yes, one display per 74HC595 (there aren’t any spare pins).
      Haven’t tried it with common-anode, but yes binary inverse etc.

      Comment by John Boxall | December 22, 2011 | Reply

      • Thanks John, tried it, worked pretty OK, but I was a bit disappointed by the output of the example sketch. Seemed that the 7segment first started to count the odd numbers and then the even numbers and it was really flickering a lot, not really the bright clear numbers as in yr video
        Looking at the code it just shld have displayed all the numbers first going up and then down.
        Maybe it is a timing issue with the 595. What I did notice is that changing the delays in yr code had quite an influence on any numbers being shown at all.
        As I have learned first to seek the faults at my end first. I have checked all the connections (good) and checked putting direct current (direct as in ‘without any arduino or 595′, just with a battery and resistors) and the the numbers were perfectly clear. I changed the code to just show my one number and that was fine for each number, but sequencing it (counting up and down) just did not go well..

        Hmm. Too bad. I am probably overlooking something.

        Nevertheless, I wld never have come this far without yr excellent tutorial

        Comment by ed | December 25, 2011

      • Sorry it’s difficult to say what is wrong without being there. Did you try https://sites.google.com/site/tronixstuff/home/arduino-tutorial-series-files/exercise5p1.pde ?

        Comment by John Boxall | December 25, 2011

  4. Thanks for yr sugegstion John. I suspect that a faulty wire hooking up the RST to pin 10 might have been the reason. After replacing that wire it went like .. eh.. clockwork

    Comment by ed | December 26, 2011 | Reply

    • Good news. It’s always the little things that hold people up.
      Have fun

      Comment by John Boxall | December 26, 2011 | Reply

  5. Exercise 5.1
    I have a small breadboard so I could not fit eight resistors on it
    so I used two resistors from pins 3 -8 as per your display to ground
    this seems easier than eight resistors and works well
    will this be ok
    thanks

    Comment by Garry | February 18, 2012 | Reply

    • There is a theoretical risk of too much current, if you were building a permanent version then use all the resistors. But for short term experimenting you’ll be fine.
      cheers
      John

      Comment by John Boxall | February 19, 2012 | Reply

  6. Hi
    interesting tutorial ,thank you for it .

    u ‘re used Common cathode seven seg,,unfortunately i didn’t have one ,but I reprogramming arduino on Common anode 7 seg, using your idea “truth table” I successed on it , just i change the following code

    int segdisp[10] = {192,249,164,176,153,146,130,248,128,144 };

    all thing worked well

    thank you again

    Comment by mohannad rawashdeh | February 25, 2012 | Reply

    • Nice work and good on you for having a go.
      Cheers
      John

      Comment by John Boxall | February 26, 2012 | Reply

  7. if the numbers you send are integers with shiftout then they are 16 bits. why does it send 8 bits. could the array be declared byte that stores the code to send to the 7 segment displays.

    Comment by archie rowley | July 8, 2012 | Reply

    • From memory shiftOut() only sends 8 bits. You could certainly use byte as you mentioned.

      Comment by John Boxall | July 23, 2012 | Reply

      • many thanks. having worked through some of your tutorials i realised that the best way to find out something is to try it, which i did. more enjoyable than just asking questions

        Comment by archie | July 23, 2012


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 3,835 other followers

%d bloggers like this: