Getting Started with Arduino – Chapter Twelve

This is part of a series titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back

This chapter we will spend some more time with the rotary encoder by using it to control a clock, look at ways of driving a common-anode LED display with Arduino, and make a usable alarm clock with which we can start to move from the prototype stage to an actual finished product – something you would be proud to give to someone.

So off we go…

In chapter eleven, we looked at getting some values from the rotary encoder. Not the easiest way of receiving user input, but certainly interesting. This week I have an example for you where the encoder is used for setting the time of a digital clock. This example is basically two previous projects mashed together. It consists of the LED digital clock from exercise 7.1, and the rotary encoder sketch from example 11.2. The sketch was quite simple in theory, but somewhat complex in execution. The idea was to read the decoder, and after every read, display the time. However, if the encoder’s button was pressed, the time set function would be activated. At this point, you turn the encoder in one direction to set the hours, and the other direction to set the minutes. Then press the button again to set that time and return to normal operations.

To recreate it you will need:

  • Your standard Arduino setup (computer, cable, Uno or 100% compatible)
  • Seven 560 ohm 1/4 watt resistors
  • Four 1 kilo ohm 1/4 resistors
  • Four BC548 NPN transistors (if you cannot find these, you can use 2N3904)
  • Two 74HC595 shift registers
  • DS1307 timer IC circuit components (see this schematic from chapter seven) or a pre-built module
  • Solderless breadboard and connecting wires

Here is the sketch for your perusal, and the matching schematic (sorry, I forgot to add the DS1307 module – see example 12.2 schematic below for how to do this):

example12p1small

… in real life:

example12p1boardsmall

and a video clip:

After watching that clip you can soon see that there is an issue with the encoder. As a normal switch can bounce (turn on and off very very quickly in the latter part of operation), so can a rotary encoder. That is why it would sometimes return a result of clockwise, instead of anti-clockwise. Furthermore, they are right little pains when trying to use in a breadboard, so if you were going to use one in greater lengths, it would pay to make up your own little breakout board for it. Therefore at this stage we will leave the encoder for a while.

You may also have noticed the extra shield between the real time clock shield (yellow) and the arduino board. It is the Screwshield for Arduino – reviewed here. It is very useful to making a stronger connection to the I/O pins, or using normal multi-core wires.

pinsborder

Next on the agenda is the common-anode LED display. Normally the LED display we have demonstrated in the past has been common-cathode, and very easy to use. Current would flow from the power supply, through the shift register’s outputs (for example the 74HC595), through current-limiting resistors, into the LED segment, then off to earth via the cathode. Current flows through a diode from the anode to the cathode, and finally back to earth/ground. For a refresher on diodes, please read this article. The other month I found this style of useful LED display:

clockdisplaysmall

Absolutely perfect for our clock experimentations. A nice colon in the middle, and another LED between the third and fourth digit which could make a good indicator of some sort. However the one catch (always a catch…) is that is was common-anode. This means that current starts from the power supply, through the common anode pin for the particular digit, then the LED segment, the LED’s individual cathode pin, through the current-limiting resistor and then to ground. With the current flowing in the opposite direction via a common anode, we can’t just hook the display up to our 74HC595 shift register.

Therefore, we will need the shift register to control switches to allow the current to flow through each segment, just like we have done previously controlling the cathodes of a common cathode display (see example 12.1). So to control the digits of this new display, we will need twelve switches (eight for the segments of the digit, and four to control the anodes). That would mean twelve BC548  transistors and 10k ohm resistors, and a lot of mess.

Instead we will now use the 74HC4066 quad bilateral switch IC. I have reviewed this chip being used with Arduinos in a separate article here. The 74HC4066 is quite a common chip, available from many suppliers including: element14/Newark (part number 380957), Digikey (part number 568-1463-5-ND) or Mouser (771-74HC4066N). If you cannot find them, email me and I can sell you some at cost plus postage. Once you have a understanding of this IC, please consider the following circuit:

example12p2schematic

Most of this should be easily understood. One shift register is controlling the anodes, turning them on and off via a 74HC4066. In past examples this shift register would have turned off common cathodes via a 10k resistor and an NPN transistor. The other shift register is controlling the individual LEDs for each digit via a pair of 74HC4066s (as they only have four switches per IC).

Here is the sketch, it should be quite a familiar piece of code for you by now.

To recreate it you will need:

  • Your standard Arduino setup (computer, cable, Uno or 100% compatible)
  • Seven 560 ohm 1/4 watt resistors
  • DS1307 timer IC circuit components (see this schematic from chapter seven) or a pre-built module
  • Two 74HC595 shift registers
  • Three 74HC4066 quad bilateral switch ICs
  • Solderless breadboard and connecting wires
  • LED clock display module

And here is the result, with red and a blue display.

And the usual board layout:

example12p2boardsmall

The blue looks really good in a dark background. You can also get them in yellow and green.

LEDborder

Moving along. Now and again, you often want to have a few buttons in use for user input, however the cheap ones don’t really like to sit in a breadboard. Naturally, you could make your own “button shield”, which would be very admirable, but then it would be preset to certain pins, which could interfere with your project. I had the same problem in writing this chapter, so came up with this example of an external “button panel” to make life easier. Here is the schematic, nothing complex at all – just four buttons and the required 10k ohm pull-down resistors:

example12p3schematic

and the finished product:

example12p3small

This was a quick job, as I will need to use a few buttons in the near future. Have also put some rubber feet on the bottom to stop the solder joints scratching the surface of the bench. Originally I was going to chop off the excess board at the top, but instead will add some LEDs to it after finishing this article. However using this button board will save a lot of frustration by not trying to jam the buttons into a breadboard.

pinsborder

Exercise 12.1

Now it is time for you to do some work. From this chapter onwards, we will be working on making a small alarm clock – something you could use. Just like the six million dollar man, we have the capability, the technology, and so on … except for Steve Austin. So this chapter, your task is to create and breadboard the  circuit and the underlying sketch. Using the LED display from example 12.1, your clock will have a menu option to set the time, alarm time, turn on and off the alarm, a snooze button – and also switch the display on and off (so you don’t stare at it when you should be trying to sleep).

You could either use a DS1307 module, or the raw parts. For an explanation of the circuitry, please see this post about making a RTC shield. You can always change it when we get to making a real prototype. The same with the Arduino – but for this exercise just stick with the normal board. Later on we will use a bare circuit the same as in chapter ten. With regards to user input, it’s up to you. A rotary encoder could be a real PITA, my example below just uses buttons. Anyhow, off you go!

Parts you will need:

  • Your standard Arduino setup (computer, cable, Uno or 100% compatible)
  • Seven 560 ohm 1/4 watt resistors
  • DS1307 timer IC circuit components (see this schematic from chapter seven) or a pre-built module
  • Two 74HC595 shift registers
  • Three 74HC4066 quad bilateral switch ICs
  • Four normally open buttons or a board as described in example 12.3
  • Solderless breadboard and connecting wires
  • LED clock display module

Here is my interpretation of the answer to the exercise. Although this is a particularly long sketch for our examples, it is broken up into many functions which are quite modular, so you can easily follow the flow of the sketch if you start at void loop(). All of the types of functions used have been covered in the past tutorials. In then next chapters we will add more functions, such an an adjustable snooze, selectable blinking colon, and so on. If you have any questions, please ask.

The buttons have several functions. In normal clock display mode, button one is for menu, two turns the alarm on, three turns it off, and four turns the display on and off. If you press menu, button two is to select time set, three for alarm set, and four is like an enter button. When in the time/alarm set modes, button one increases the hour, button two increases minutes in units of ten, and button three increases minutes in ones, and four is enter. When the alarm activates, button four turns it off.

The schematic is just example 12.2 and example 12.3 connected together, however the first button on the external board is connected to digital pin 8 instead of 1.

So here is a photo of our work in progress:

exercise12p1boardsmall

And a video clip showing the various functions of the clock in operation:

I hope you found success and inspiration in this chapter. Now to Chapter Thirteen.

LEDborder

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, or join our 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 tronixstuff.com.

11 Responses to “Getting Started with Arduino – Chapter Twelve”

  1. Sonny says:

    I’m still a little confused on how to control a rotary encoder. So it will just keep adding as you turn it the same direction? How do you get it to ignore the up or down signal after a certain point? Say I want it to ignore signals after ten latches? I want it to recognize ten switch signals than ignore the rest coming in. than the same for down back to zero?

  2. Keegan says:

    It seems to me that the shift register should be able to sink current for the individual elements.

    • John Boxall says:

      This is true, however at the time I was experimenting with 74HC4066s and felt like using one.
      Slowly I am reviewing the tutorials and will update it in the near future.
      Thanks for reading
      john

  3. Jeff Shepard says:

    Great stuff! I managed to use common anode 7 seg single digit displays using just the shift registers to count up to 99 and back down. All I did was change the base=10 codes to invert the 1′s and 0′s. So to clear the digits I would send 255 and not 0. It seems to be working just fine. Could I do the same thing here? Well I have some of these displays on the way from china so in a few weeks I will know if I can or not. This has been the best help so far, keep up the good work.

    Jeff

  4. Jeff Shepard says:

    I was referring to example 5.1 using two 595 shift registers. Instead of the shift register sending out a 1 to turn on a segment it now sends out a zero. I have also hooked up two buttons to make it count up and down. I have been going through all of your exercises one by one and I have learned so much. It just seems that all the led’s I have, be it 7 segments or 8×8 matrixes, are all common anode. It has been rather frustrating trying to change the wiring and/or the code, but I seem to have worked it out with these two segments. I just don’t know if the 595 can sink all 4 segments of my common anode display. I guess I should become a member and stop lurking on this web site. Thanks again John.

    • John Boxall says:

      Oh OK. If your displays are common-anode, life will be much easier if you use TLC5940s instead – see http://wp.me/pQmjR-Ey
      We don’t have site membership however if you are enjoying the articles kindly consider a donation – see “How you can help” at the top of any page.
      have fun
      john

  5. Jeff Shepard says:

    I didn’t like how it would blink “OFF” for a whole minute before going back to the time, so I lowered the blink cycle from 50 to 2 and it fixed the problem just fine….I have just figured out why programmers have a high suicide rate….when you fix one problem, you create another….

    The alarm would keep comming back on after I hit button 4 for the next minute. That was because the alarm check was still valid for 60 seconds. By adding “+seconds” to “minute” the alarm check is only valid for a second and fixed the problem. I hope this helps anybody who comes along in the future.

    from

    if (alarmhour==hour && alarmminute==minute)

    to

    if (alarmhour==hour && alarmminute==minute+second)

    Hey John, is there a way to save the alarm set time to the DS1307 so if the power goes out it will keep the alarm set time?

Trackbacks/Pingbacks

  1. [...] This post was mentioned on Twitter by Arnon Katz. Arnon Katz said: Getting Started with Arduino – Chapter Twelve …: This chapter we will spend some more time w… http://bit.ly/clpqbD #Arduino #Buzzduino! [...]


Leave a Reply

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: