Tag Archive | "rotary"

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.

Posted in 4066, 74HC4066, arduino, COM-09117, COM-09481, COM-09483, education, learning electronics, lesson, microcontrollers, tutorialComments (11)

Getting Started with Arduino – Chapter Eleven

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

Welcome back

In this instalment we will start to investigate radio data transmission; then introduce rotary encoders.

As technology has marched along and generally improved on the past, things have been getting small and relatively cheaper. What was once jaw-droppingly amazing is now just meh. But as you are reading this, you know differently. We can now take control of this technology for our own devices. What has been in the past quite unapproachable is now relatively – the concept of wireless data transmission. But no just sending a signal, like a remote-control garage door opener – but sending actual,  useful data – numbers and characters and so on.

How is it so? With this pair of tiny devices:

txrxmodule

Quite small indeed – the pins are spaced 2.54mm apart, and the graph paper is 5mm square. The transmitter is on the right. This pair operates at 315 kHz, over up to a theoretical 150 metres. The data transmission speed is 2400 bps (bits per second). These units are serial passthrough, that is they can replace a link of wire between the serial TX (pin 1) from one Arduino, and the RX of another Arduino (pin 0). They don’t need aerials for very short distances, but the use of one will extend the range towards the maximum. And finally, the transmitter needs between 2 and 10 volts; the receiver 5. Here is the data sheet for these modules: 315MHz.pdf. Normally they are sold individually, for example the transmitter and receiver. You can also find faster (data speed) modules, however we will use these ones today.

In preparation to use these modules, another library needs to be installed – the VirtualWire library. Download the latest revision from the following location: http://www.open.com.au/mikem/arduino/.

There is also a guide to using the library in .pdf format as well. Please note the library author’s instructions with regards to licensing on the last page of the guide. For a refresher on how to install a library, please head back to chapter two. This library will save us a lot of time, it takes care of checking for errors, and only allows complete, correct data (i.e. what is received matches what is sent) to be used in the receiver’s sketch.

However, as wireless is not 100% reliable, you need to take into account that transmissions may not be received, or erroneous ones will be ignored by the receiver’s sketch. You can reduce the data speed to improve reliability and range. Furthermore, you cannot use PWM on D9 and D10 if you are using VirtualWire.

Therefore if you are using this for some important communications, have the transmitter repeatedly sent the message. Later on in this series we will investigate more powerful solutions. Anyhow, moving along …

First of all, we will demonstrate the use of these modules with a basic sketch. It sends some text from one Arduino to another. The receiving Arduino sends the data to the serial monitor box. Of course you could always use an LCD module instead. In my own inimitable style the sketches are very simple, yet allow you to use their contents in your own work. Here is the sketch for the transmitter – tx.pdf and the receiver – rx.pdf.

When working with two sketches at the same time, you can have two Arduinos connected to your PC simultaneously,  just remember to select the correct USB port for the correct Arduino. Do this with the tools > serial port menu option in the IDE. Otherwise you will become very frustrated if you upload the rx sketch to the tx Arduino.

Furthermore, you will need to remove the wire from digital 0 to the data pin on the receiving units before uploading the sketch. And finally, remember to set the serial monitor window at 9600 baud.

Here are my two boards in action:

example11p1small

Although having both boards connected to the one computer is only useful for demonstration purposes, in real life this is obviously useless. Remember that once you upload your sketch the Arduino doesn’t need a computer, only a power supply. You can feed yours between 7 and 12 volts DC through the socket. A nice switchmode power pack will do nicely, or if you are a cheapskate like me, a PP3 battery and clip soldered to a DC plug:

pp3small

You may find that when you use a battery powered Arduino that it basically does not work. Arduino genius Jon Oxer (co-author of Practical Arduino) has found a solution for this issue – place a 10k resistor between GND and digital 0 (RX), or between digital pins 0 and 1. The next thing to consider it improving the reception range. This can be done using two methods – the first by connecting an external antenna, either a length of wire, or perhaps a purpose-built aerial. The second method is to increase the supply voltage of the transmitter up to 12 volts.

Now it is your time to do some work:

Exercise 11.1

You now are able to send characters using the radio link from one Arduino to another. Now it is time to control things remotely. For the purpose of the exercise, we will just control three LEDs, turning them on and off. You already know how to control other things with digital output pins, so we just need to focus on getting the switching on and off. Hint – you can send characters via the wireless link, so create your own codes.

You will need:

  • Two standard Arduino setups (computer, cable, Uno or compatible)
  • two breadboards and some connecting wire
  • One transmitter and one receiver unit
  • three LEDs
  • 3 x 560 ohm 0.25 W resistors. They are to reduce the current to protect the LEDs

Here is the schematic of my interpretation:

exerc11p1sch

… the transmitter:

exer11p1txsmall

… the receiver:

exer11p1rxsmall

and the video:

So how did you go? Hopefully this exercise was easier than you had first expected. If not, here are the example sketches: exercise 11.1 tx and exercise 11.1 rx. A basic transmit/receive system like this would also be handy for testing the range that wireless modules can operate over, or testing a particular site to see if you could implement such wireless modules. It’s always better to test before agreeing to make something for someone.

That concludes our work with radio wireless links – for now.

LEDborder

Next on the agenda is the rotary encoder. Recall how we used a potentiometer in the previous chapters as a dial, to select menu options using the readdial() function. It was simple, cheap and it worked, but some may say it was a kludge. There must be a better way! And there is, with the rotary encoder. A rotary encoder looks like a potentiometer, but it is a knob that can be rotated in either direction infinitely. Furthermore, the knob is also a normally-open button. The encoder we will be using in this chapter is a 12-step encoder, in that you can feel it physically resist rotation slightly twelve times over the 360 degrees of rotation.

Here is our example:

encoder11

On one side there are three pins, and two on the opposing side. On the perpendicular sides are legs for strength, that is they are meant to be soldered into a PCB to lock it in nicely. The problem for us is that those legs interfere when trying to use the encoder in a breadboard, so I have bent them up and cut them off:

encoder2

The pins are easy to understand. The two pins on one side are the button contacts, just like any simple button. The other side with the three pins – the centre goes to ground, and the outside pins are the forwards and backwards output pins. The data sheet for our encoder is here. After fooling about with this all afternoon, the quickest way to get a feel for how it works is with a simple demonstration. So first we will test it out, then see how we can use it in our user-interfaces.

This example is very easy to assemble. You only need an encoder, and the usual Arduino setup. Here is the sketch, and the schematic:

example11p2schem

and in real life:

example11p2boardsmall

and finally a snapshot of the output. Don’t forget to set the speed in your serial monitor box to 115200 baud:

example11p2screen

So as you can see, this is a much better solution that then potentiometer that we used in the past. Plus having the button integrated in the encoder is very convenient, you can really create a user interface with only one control. In the next instalment of this series we will implement the encoder in an existing design. So on to Chapter Twelve.

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.

Posted in arduino, COM-09117, learning electronics, lesson, microcontrollers, rotary encoder, tutorial, WRL-10532, WRL-10534Comments (25)


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: