Getting Started with Arduino! – Chapter Six

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 is here.

Welcome back fellow arduidans!

Hello once again to our regular Arduino tutorial instalment. In this chapter we are up to all sorts of things, including: distance sensing, using prototyping shields, even more shiftiness with shift registers and 4-digit 7-segment displays, and some exercises to refresh and expand your knowledge. Wow – let’s get cracking…

Do you know how to keep your distance? Some people do, some do not. The same goes for mechatronical things (i.e. robots, those little autonomous vacuum cleaners, etc). So to solve that problem you can integrate a distance sensor into your design. Hopefully by following this section you will gain an understanding of such sensors, and be able to make use of them later on in your own projects. Anyhow, today we are looking at the Sharp GP2Y0A21YK0F infra-red distance sensor. What a mouthful… The funny thing is that it looks like a robot head:


That white JST connector is for three leads, +5V, GND, and analogue out. When purchasing it you should also get a matching lead to save time and mucking about.

How it works is quite simple (I must stop writing that, some things are not as simple as they seem…) – the sensor contains an infra-red transmitter and a receiver. It returns a voltage that is relative to the distance between itself and the object in front of it. This output voltage is inversely proportional to the distance; which is explained better with this graph from the data sheet:


However it is always fun to test these things out yourself. So I placed a sensor up over my desk, measured out 80cm, and attached the multimeter to the analogue output of the sensor:


A crude setup but effective. I held a white piece of cardboard in front of the sensor, starting from more than one metre away, then moved closer to the minimum, then back out again. As shown in this video clip:

Although watching that multimeter may not have been too interesting, hopefully the next task will be!

Exercise 6.1

Using the values from the graph from the Sharp data sheet (above), make yourself a distance-measuring device. Use an LCD module to display the measurements. Metric and imperial! This shouldn’t be too hard at all, you’re just using one analogue input from the sensor, some basic maths, and displaying some data on an LCD. Furthermore, to make it truly portable, you could wire up a 9v PP3 battery to a DC plug and use it for power. A hint – before calculating distances, run a sketch to just return the analogRead() value from the sensor. Then you can make your own judgement on voltage-distance calculations. To save time I used the Electronic Bricks to rapidly construct this prototype.

You will need:
  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • One parallel LCD display module
  • One Sharp infra-red distance sensor and sensor cable
  • a breadboard and some connecting wire

Anyhow, here is a photo of what I came up with:


and the ubiquitous video clip

Finally, my sketch for the solution. You may have to adjust the values in the decision tree for more accuracy. After spending some time with this sensor, I wouldn’t rely on it for exact distance calculations, however it would be very useful for general item detection, air switches and so on. In the next week or two we will examine another type of distance sensor.

What else could this be used for? Robotics sensors, burglar alarms, switching things on and off. Hopefully you have gained some knowledge about this sensor and have some ideas for implementation.


Now that we have spent a few weeks constructing our prototypes on breadboards and electronic bricks, it is now time to look at how we can do this in a more compact, and/or permanent method. As you already know, the Arduino system allows for “shields”, PCBs that plug on top of your Arduino board to add some sort of extra functionality. One of these was the Electronic Brick chassis, another popular shield is the Ethernet shield.

Moving on…

In previous instalments we have worked with 7-segment LED displays, using up to three at once, being controlled by 74HC595 shift registers. As you may have realised by now that involved a lot of wiring, resistors, time and effort. But what if you need four or more digits? Use an LCD… Maybe. Sometimes you need to use LED displays for aesthetic reasons, price, clarity, or just because you love that LED look. Thankfully you can find four digit displays, instead of having to use 2 x 2 or 4 x 1 digit displays. Let’s have a look at one now:


For the newcomer there would be a surprising lack of pins on this display module. That is a good thing, and a slightly tricky thing – but we can overcome the obstacles and use it very easily. How? Again, with two 74HC595 shift registers and some brainpower. Firstly, let’s have a look at the pins – from left to right they are: E, D, C, G, F, B, A, C1, C2, C3, C4, decimal point, unused, unused. This display is common cathode, so to display (for example) the number 1 on digit 3, you would apply ~+2 volts to pins 6 and 7, and attach ground to pin 10. Very much the same as using a single-digit display, except you need to choose the cathode that corresponds with the digit you wish to use. In this tutorial we are using a Common Cathode unit. Out of curiosity’s sake, here is the data sheet for the module used in this chapter: data sheet.pdf.

Secondly, how are we going to control the cathodes with out Arduino? Current comes out of a cathode, so it would not accept a signal from our digital out pins. What we need to do is have a simple switch on each cathode between the display pin and ground, so we can control which digit we want to use. How can we do this with our Arduino? Easy… we can use a simple NPN transistor as a switch. Remember we did this with a relay in chapter three!

But using 4 digital out pins for cathode control is a waste of pins, we can use our trusty shift register again to control the cathodes. So that means we need two shift registers in total, the first to control the digit (0~9), and the second to switch on the cathode of the position we wish to display our digit in. Time to do it!

The first (left-hand) shift register from the Arduino controls the segments on the display, via 560 ohm resistors. Just like last time. The second (right-hand) shift register controls the cathodes. Pins Q0~Q3 connect to the base of a BC548 transistor via a 1k ohm resistor. The collector of the transistor is connected to the cathode on the display, and the emitter to ground. For example:


Note that the schematic above is a guide only. But here it is in real life, below:


After a few projects, wiring up displays and shift registers should be a lot quicker for you now. Here is the matching sketch I came up with for the demonstration video below.

You’d have to admit, even in the year 2010, LED displays still look mesmerising. Or maybe that’s just me! Here is the data sheet display.pdf for the LED display I used. You can use other ones,as long as they are common cathode; just match the LED element pins with your first shift register, and the cathode pins with the second shift register.

But on to making something useful…

Exercise 6.2

Using the hardware from example 6.1 above, create a device that displays the value of an analogue sensor. For example, if we connect a 10k variable resistor to an analogue input, the Arduino will return a reading of between 0 and 1023. From a hardware perspective, all you need to do is add an analogue sensor (e.g. LDR, 10k variable resistor, the infra-red sensor from earlier on, etc.). The software will be a little tricky, but if you completed exercise 5.1, or 5.2 you shouldn’t have a problem at all. As you will be displaying one digit at a time, but very quickly, try to minimise the number of times you clear the display – in doing so you will keep the brightness at a maximum.

You will need:

  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • One 4-digit, 7-segment LED display, common cathode
  • Two 74HC595 shift registers
  • Four BC548 or equivalent NPN transistors
  • 8 x 560 ohm 0.25 W resistors. For use as current limiters between the LED display segments and ground
  • One 10k variable resistor
  • a breadboard and some connecting wire

For motivation, here is a video clip of my result. There are two examples, one with leading zeroes, and one without:

And the sketch as well.

That wasn’t too hard was it? Now that you can use such a display, it will become easier to display output from your various projects. Now on to Chapter 6A.


In the meanwhile have fun and keep checking into 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.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of

31 Responses to “Getting Started with Arduino! – Chapter Six”

  1. salsa says:

    Might want to check out SAA1064 ICs– i2c driver for 4-digit 7-segment LED drivers (common anode), so easy to work with! Constant current output, four on a bus, some Arduino examples out there:

  2. You can get very precise readings if you add a capacitor to the signal line and build a lookup table for readings.

    The script on the link below generates the table:

  3. B0SC0 says:

    Just a minor heads-up.

    The NPN Transistor is drawn wrong.

    Also, how about using a couple of ULN2803'S OR ULN2003'S?

    Dip IC with 8 or 7 darlington common emitter transistor array.

    Great website by the way. I am learning a lot.

    Thanks and keep up the good work.


  4. Michael says:

    Hi John. In sketch 6.2 I have not found where the valve of leadingzero =0 or =1. Please show me.

  5. mr.oiz0 says:

    Excellent work! Been reading your tutorials and i must say that they are very helpful!

    Thank you!

  6. bkchaturvedi says:

    Document is useful. Can i have the interconnection diagram Arduino-74HC595-7-Segment LED’s. Sketch for running the show is also needed.



  7. Kyle says:

    Hey, great tutorial! I’m not sure if it’s just me or not, but when I download example6p1 it’s a sketch about lcd’s instead of the 7 seg display. I’m having a bit of trouble understanding how the shift registers work and testing my circuit without it. Thanks a lot!

  8. Konstantinos says:

    Hi John,

    Nice and interesting site.

    I simply cannot download the sketches for the examples of chapter six.
    Can you e-mail them to me?
    Thanks a lot

  9. tubzster says:

    Once more John thanks very much for your detailed tutorials and your prompt response with the link.

    I have a question for you.

    I am trying to connect a 4digit seven segment display board with two 74HC595 to an Arduino Uno.
    The disp is common cathode and has a different pinout from the one you are using.
    However the segments (a,b,c,d,e,f,g) and dp are correctly mapped to the Q0-Q7 of the 595, while the 4 c.c. through 4 npns are connected to the Q4-Q7 of the other 595, not Q0-Q3 as you are doing in yours.
    In order to make it work I have therefore changed the decimal values of “posdisp[4]” from 1,2,4,8 to 16,32,64,128.
    That is fine.

    But I am confused a bit with the decimal values for “segdisp[10]”.
    According to your explanation in chapter 5 to turn on the numbers 0-9 in a segment you result to decimal numbers 63,6,91,79,102,109,125,7,127,111.
    This is I assume standard for c.a. so in order to turn on c.c. you must control the “off” segments, i.e. 64,121,36,48,25,18,2,120,0,16 with dp “on” or add 128, i.e. 192,249,164,176,153,146,130,248,128,144 with dp “off”.
    Is my thinking correct or not, as in your examples the decimal values for “segdisp” are different as indicated below.

    Exercise 6.2
    Display analogue sensor readings
    with 4 x 7-segment module
    and two 74HC595 shift registers

    int segdisp[10] = {
    238,72,214,220,120,188,190,200,254,252}; // base 10 equivalents for digits 0~9
    int posdisp[4] = {
    1,2,4,8}; // base 10 equivalents to close cathodes on display 0~3 on module

    Exercise 6.2addendum

    int segdisp[10] = {
    123, 72,103,79,92,31,63,74,127,95}; // base 10 equivalents for digits 0~9
    int posdisp[4] = {
    1,2,4,8}; // base 10 equivalents to close cathodes on display 0~3 on module

    Can you give me some more details about this issue.

    Thanking in advance,

    Best regards


  10. Brandon Landesman says:

    Hey John!

    Love the info I’m getting from your tutorials. I’m kind of bouncing around and picking things up here and there. I’m building/designing a 4digit 7 segment setup to read a value from either a pot or encoder (leaning towards encoder) that is attached to a wheel to give precise measurements of forward and backwards rotation. I’m having issues with the pot not giving very accurate readings. I’ve read a couple of places about putting a cap to help clean up the noise and/or building a “debounce” into the code. Your take on why mine is acting that way and your idea on a fix? I’m more about understand why something happened and how to fix it then a simple “here’s your fix”. Keep up the amazing tutorials!


    • John Boxall says:

      Use a rotary encoder – it offers an accurate measure of rotation. Potentiometers are cheap and simple, however they can be rough and troublesome for digital applications.

  11. Brandon Landesman says:


    I’ve got my code dialed in and debounced and I’m liking it, but I want to try and change my value from integer to floating. How would you recommend going about doing that? How do you set the precision in your code to the 2nd decimal place (2nd from right…3rd digit?)


  12. Ben says:

    I think there is a cheaper way to build permanent projects. Instead of buying and using proto shields, why not buy a sheet of “veroboard’ You can buy them in different sizes and cut them to the size required, no waste of board and no waste of space.

  13. Pete G says:

    Bit confused about the references to “8 x 560 ohm 0.25 W resistors. For use as current limiters between the LED display segments and ground”. These have cropped up a bit, in the “you will need” lists. Do you actually mean, connected between the 74HC595 outputs and the segment pins.

    I’m planning to mod the code slightly to use 2 * 4 digit modules to give 8 digits. As the 74HC595s have 8 outputs, there should be no probs to doing this, apart from the duty cycle of each digit is now only 1:8 instead of 1:4.

  14. Pete G says:

    Funny you should mention the MAX7219. I’ve been looking at what libs/code was out there, and the 7219 came up a lot, so I’ve ordered a few to play with today.

  15. Roufianos Roufianou says:

    Electronic Bricks site you posted is down, is anything else we can use?


Leave a Reply

Subscribe via email

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

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

Use of our content…

%d bloggers like this: