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:

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. Furthermore, 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 continue with more in the next tutorial.
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.

The following two tabs change content below.

John Boxall

Person. Author of http://arduinoworkshop.com Director of http://tronixlabs.com.au Rare updater of http://tronixstuff.com VK3FJBX

17 Responses to “Getting Started with Arduino! – Chapter Five”

1. Mr Guy says:

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…

2. Russ says:

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

• John Boxall says:

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

3. ed says:

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?

• John Boxall says:

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.

• ed says:

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

• John Boxall says:

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 ?

4. ed says:

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

• John Boxall says:

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

5. Garry says:

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

• John Boxall says:

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

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

• John Boxall says:

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

7. archie rowley says:

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.

• John Boxall says:

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

• archie says:

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

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.