Tag Archive | "iic"

Tutorial: Arduino and multiple thumbwheel switches

This is an addendum to chapter forty of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here. Any files from tutorials will be found here.

Updated 24/11/2012

This article continues with the push-wheel switches introduced in chapter 40. In the previous article, we learned how to read the value of a single digit using the digital pins of our Arduino. With this instalment we will examine how to read four digits – and not waste all those digital pins in the process. Instead, we will use the Microchip MCP23017 16-bit port expander IC that communicates via the I2C bus. It has sixteen digital input/output pins that we can use to read the status of each switch.

Before moving forward, please note that some assumed knowledge is required for this article – the I2C bus (parts one and two) and the MCP23017.

We first will describe the hardware connections, and then the Arduino sketch. Recall the schematic used for the single switch example:


When the switch was directly connected to the Arduino, we read the status of each pin to determine the value of the switch. We will do this again, on a larger scale using the MCP23017. Consider the pinout diagram:

We have 16 pins, which allows four switches to be connected. The commons for each switch still connect to 5V, and each switch contact still has a 10k pull-down resistor to GND. Then we connect the 1,2,4,8 pins of digit one to GPBA0~3; digit two’s 1,2,4,8 to GPA4~7; digit three’s 1,2,4,8 to GPB0~3 and digit four’s 1,2,4,8 to GPB4~7. For demonstration purposes we are using the Gravitech 7-segment shield as reviewed in the past.

Now how do we read the switches? All those wires may cause you to think it is difficult, but the sketch is quite simple. When we read the value of GPBA and B, one byte is returned for each bank, with the most-significant bit first. Each four bits will match the setting of the switch connected to the matching I/O pins.

For example, if we request the data for both IO banks and the switches are set to 1 2 3 4 – bank A will return 0010 0001 and bank B will return 0100 0011. We use some bitshift operations to separate each four bits into a separate variable – which leaves us with the value of each digit. For example, to separate the value of switch four, we shift the bits from bank B >> 4. This pushes the value of switch three out, and the blank bits on the left become zero. To separate the value for switch three, we use a compound bitwise & – which leaves the value of switch three.

Below is a breakdown of the binary switch values – it shows the raw GPIOA and B byte values, then each digit’s binary value, and decimal value:

So let’s see the demonstration sketch :

And for the non-believers … a video demonstration:

So there you have it. Four digits instead of one, and over the I2C bus conserving Arduino digital I/O pins. Using eight MCP23017s you could read 32 digits at once. Have fun with doing that!


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, education, I2C, lesson, MCP23017, microcontrollers, push wheel switch, tutorialComments (2)

Review: Gravitech 7-Segment Arduino Shield

Hello Readers

In this article we examine the “7-Segment Arduino Shield” received for review from the team at Gravitech in the United States. This is an Arduino Uno/Duemilanove-type compatible shield that contains four very useful items:

  • Four 7-segment LED numerical displays – driven by the NXP SAA1064 LED display driver IC;
  • A large 10mm RGB LED;
  • A Microchip 24LC128 EEPROM, and
  • A TI TMP75 digital temperature sensor.
Apart from the LED all the other components are controlled via the I2C bus. So as well as being generally useful for experimenting, monitoring temperature and so on, this is an ideal board for Arduino and I2C bus practice. (If you have not done so already, consider reading our I2C tutorial, part one and two). Let’s look at the hardware, then move on to using the features.
As with other Gravitech products, the shield arrives in a reusable static shielding bag:
and here we have it:
The IC at the top-left of the shield is the TMP75 temperature sensor, bottom-left is the 24LC128 EEPROM, and the whopper below the first two digits is the NXP SAA1064. The shield layout is very neat and clean, and the white finish is a pleasant change compared to the usual black or green Arduino shields out there. The PWR LED is a blue colour. The only issues I found were that you cannot use this with a Mega due to the location of the I2C pins, and the component leads were not trimmed at the factory, which caused an issue when the shield was inserted into an Ethernet shield. This is easily solved by clipping the leads yourself:
Here is the shield in operation using the supplied demonstration sketch. The temperature is displayed in Celsius, with the LED changing colour depending on the temperature:

That is all very good, but how do we use the features of the board? Let’s look at each of the aforementioned features individually. First of all, the numeric display. The four seven-segment LED displays are controlled by the NXP SAA1064 LED display driver (data sheet (.pdf)). I have written a separate tutorial on how to use this IC, and it is completely compatible with this shield. So visit the tutorial here and put the numbers to work! Please note the I2C bus address for the SAA1064  is 0x38.

Next we have the RGB LED. Red, green and blue are connected to digital pins 3, 5 and 6 respectively. These are also pulse-width modulation pins, so you can have altering the brightness. Here is a simple demonstration sketch:

And for the curious, here it is in action:

Next, the Microchip 24LC128 EEPROM. It has 128kbit storage space, which translates to 16 kilobytes. The I2C bus address is 0x50. Once again there is a complete explanation of how to use this sort of EEPROM in another tutorial – check it out. But for quick reference the following demonstration sketch writes the numbers 0~255 to memory locations 0~255:

Although there is 16 kilobytes of memory the sketch only writes and reads to the first 255 locations. Each location can store a byte of value between zero and 255. Here is a screen shot of the serial monitor results (click to enlarge):

And now time to work with the Texas Instruments TMP75 temperature sensor (data sheet.pdf). It has a reasonable operating temperature range of between -40 and 125 degrees Celsius – however this would exceed the range in which your Arduino is capable of working, so no leaving the shield on the car dashboard during a hot summer’s day. The I2C bus address for the TMP75 is 0x49. We will deconstruct the Gravitech demonstration sketch to explain how the temperature works.

The TMP75 needs to be initialised before measurement can take place, by sending the following data:

The temperature data is received in two bytes of data, as it spans 12 bits. Thankfully the demonstration sketch has done the work for us. Have a look at the Cal_temp() function, which converts the two raw bytes of data from the TMP75. There is some bitwise arithmetic in there, however if you are not keen on going down to that level, it is easy enough to cut and paste the temperature and numeric display functions.  Here is a quick video of the demonstration sketch in action:


So there you have it – another useful and educational shield for use with your Arduino. If you have any questions or enquiries please direct them to Gravitech via their contact page. Gravitech products including the 7-segment shield are available directly from their website or these distributors.

As always, thank you for reading and I look forward to your comments and so on. Furthermore, don’t be shy in pointing out errors or places that could use improvement. Please subscribe using one of the methods at the top-right of this web page to receive updates on new posts, follow on twitterfacebook, or join our Google Group.

[Disclaimer – the shield reviewed in this article was a  promotional consideration made available by Gravitech]

High resolution photos are available on flickr.

Posted in 24LC128, arduino, gravitech, I2C, LED, microcontrollers, product review, review, SAA1064, TMP75, tutorialComments (0)

Tutorial: Arduino and Thumbwheel switches

This is chapter forty of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here. Any files from tutorials will be found here.

[Updated 20/01/13]

In this article we go back to the past via the use of push-wheel/thumbwheel switches with out Arduino systems. Here are some examples sourced from somewhere on eBay:

For the uninitiated, each switch is one vertical segment and they can be connected together to form various sizes. You can use the buttons to select from digits zero through to nine. There are alternatives available that have a wheel you can move with your thumb instead of the increase/decrease buttons. Before the days of fancy user interfaces these switches were quite popular methods for setting numerical data entry. However they are still available today, so let’s see how they work and how we can use them. The switch’s value is made available via binary-coded decimal. Consider the rear of the switch:

We have common on the left, then contacts for 1, 2, 4 and 8. If you apply a small voltage (say 5V) to common, the value of the switch can be measured by adding the values of the contacts that are in the HIGH state. For example, if you select 3 – contacts 1 and 2 will be at the voltage at common. The values between zero and nine can be represented as such:


By now you should realise that it would be easy to read the value of a switch – and you’re right, it is. We can connect 5V to the common,  the outputs to digital input pins of our Arduino boards, then use digitalRead() to determine the value of each output. In the sketch we use some basic mathematics to convert the BCD value to a decimal number. So let’s do that now.

From a hardware perspective, we need to take into account one more thing – the push-wheel switch behaves electrically like four normally-open push buttons. This means we need to use pull-down resistors in order to have a clear difference between high and low states. So the schematic for one switch would be (click image to enlarge):


Now it is a simple matter to connect the outputs labelled 1, 2, 4, and 8 to (for example) digital pins 8, 9, 10 and 11. Connect 5V to the switch ‘C’ point, and GND to … GND. Next, we need to have a sketch that can read the inputs and convert the BCD output to decimal. Consider the following sketch:

The function readSwitch()  is the key. It calculates the value of the switch by adding the numerical representation of each switch output and returns the total as its result. For this example we used a numerical display shield that is controlled by the NXP SAA1064. If you don’t have one, that’s ok – the results are also sent to the serial monitor. Now, let’s see it in action:

Ok it doesn’t look like much, but if you need numerical entry it saves a lot of physical space and offers a precise method of entry.

So there you have it. Would you actually use these in a project? For one digit – yes. For four? Probably not – perhaps it would be easier to use a 12-digit keypad. There’s an idea…  But for now I hope you enjoyed reading this as much as I did writing it for you.

Update! See the addendum for using four switches at once to read four-digit numbers here


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, education, I2C, lesson, microcontrollers, push wheel switches, tutorialComments (8)

Tutorial: Arduino and the NXP SAA1064 4-digit LED display driver

Learn how to use the NXP SAA1064 LED display driver IC in chapter thirty-nine of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe.

Updated 19/01/2013

In this article we investigate controlling the NXP (formerly Philips) SAA1064 4-digit LED display driver IC with Arduino and the I2C bus interface. If you are not familiar with using the I2C bus, please read my tutorials (parts one and two) before moving on. Although the SAA1064 is not the newest on the market, it is still popular, quite inexpensive and easy to source. Furthermore as it is controlled over the I2C bus – you don’t waste any digital I/O pins on your Arduino, and you can also operate up to four SAA1064s at once (allowing 16 digits!). Finally, it has a constant-current output – keeping all the segments of your LED display at a constant brightness (which is also adjustable).  So let’s get started…

Here is an example of the SAA1064 in SOIC surface mount packaging:

It measures around 15mm in length. For use in a solderless breadboard, I have soldered the IC onto a through-hole adaptor:

The SAA1064 is also available in a regular through-hole DIP package. At this point, please download the data sheet (.pdf) as you will need to refer to it during the article. Next, our LED display examples. We need common-anode displays, and for this article we use two Agilent HDSP521G two-digit modules (data sheet [.pdf]) as shown below:

For the uninitiated – a common anode display has all the segments’ anodes connected together, with the cathodes terminated separately. For example, our LED displays are wired as such:

Notice the anodes for the left digit are pin 14, and the right digit pin 13. A device that is connected to all the cathodes (e.g. our SAA1064) will control the current flow through each element – thereby turning each segment on (and controlling the brightness) or off. Our SAA1064 is known as a current-sink as the current flows through the LED, and then sinks into the IC.

Now, let’s get it connected. There is an excellent demonstration circuit on page twelve of the data sheet that we will follow for our demonstrations:

It looks pretty straight-forward, and it is. The two transistors are standard NPN-type, such as PN2222. The two transistors are used to each turn on or off a pair of digits – as the IC can only drive digits 1+3 or 2+4 together. (When presented in real life the digits are numbered 4-3-2-1). So the pairs are alternatively turned on and off at a rapid rate, which is controlled by the capacitor between pin 2 and GND. The recommended value is 2.7 nF. At the time of writing, I didn’t have that value in stock, so chose a 3.3 nF instead. However due to the tolerance of the ceramic capacitor it was actually measured to be 2.93 nF:

So close enough to 2.7 nF will be OK. The other capacitor shown between pins 12 and 13 is a standard 0.1 uF smoothing capacitor. Pin 1 on the SAA1064 is used to determine the I2C bus address – for our example we have connected it straight to GND (no resistors at all) resulting in an address of 0x70. See the bottom page five of the data sheet for other address options. Power for the circuit can be taken from your Arduino’s 5V pin – and don’t forget to connect the circuit GND to Arduino GND. You will also use 4.7k ohm pull-up resistors on the SDA and SCL lines of the I2C bus.

The last piece of the schematic puzzle is how to connect the cathodes of the LED displays to the SAA1064. Display pins 14 and 13 are the common anodes of the digits.

The cathodes for the left-hand display module:

  • LED display pins 4, 16, 15, 3, 2, 1, 18 and 17 connect to SAA1064 pins 22, 21, 20, 19, 18, 17, 16 and 15 respectively (that is, LED pin 4 to IC pin 22, etc.);
  • LED display pins 9, 11, 10, 8, 6, 5, 12 and 7 also connect to SAA1064 pins 22, 21, 20, 19, 18, 17, 16 and 15 respectively.
The cathodes for the right-hand display module:
  • LED display pins 4, 16, 15, 3, 2, 1, 18 and 17 connect to SAA1064 pins 3, 4, 5, 6, 7, 8, 9 and 10 respectively;
  • LED display pins  9, 11, 10, 8, 6, 5, 12 and 7 also connect to SAA1064 pins 3, 4, 5, 6, 7, 8, 9 and 10 respectively.
Once your connections have been made, you could end up with spaghetti junction like this…
Now it is time to consider the Arduino sketch to control out SAA1064. Each write request to the SAA1064 requires several bytes. We either send a control command (to alter some of the SAA1064 parameters such as display brightness) or a display command (actually display numbers). For our example sketches the I2C bus address “0x70 >> 1” is stored in the byte variable saa1064. First of all, let’s look at sending commands, as this is always done first in a sketch to initiate the SAA1064 before sending it data.
As always, we send the address down the I2C bus to awaken the SAA1064 using

Then the next byte is the instruction byte. If we send zero:

… the IC expects the next byte down the bus to be the command byte. And finally our command byte:

The control bits are described on page six of the data sheet. However – for four-digit operation bits 0, 1 and 2 should be 1; bit 3 should be 0; and bits 4~6 determine the amount of current allowed to flow through the LED segments. Note that they are cumulative, so if you set bits 5 and 6 to 1 – 18 mA of current will flow. We will demonstrate this in detail later on.

Next, to send actual numbers to be displayed is slightly different. Note that the digits are numbered (from left to right) 4 3 2 1. Again, we first send the address down the I2C bus to awaken the SAA1064 using

Then the next byte is the instruction byte. If we send 1, the next byte of data will represent digit 1. If that follows with another byte, it will represent digit 2. And so on. So to send data to digit 1, send

Although sending binary helps with the explanation, you can send decimal equivalents. Next, we send a byte for each digit (from right to left). Each bit in the byte represents a single LED element of the digit as well as the decimal point. Note how the elements are labelled (using A~G and DP) in the following image:

The digit bytes describe which digit elements to turn on or off. The bytes are described as such: Bpgfedcba. (p is the decimal point). So if you wanted to display the number 7, you would send B00000111 – as this would turn on elements a, b and c. To add the decimal point with 7 you would send B10000111. You can also send the byte as a decimal number. So to send the digit 7 as a decimal, you would send 7 – as 00000111 in base-10 is 7. To include the decimal point, send 135 – as 100000111 in base-10 is 135. Easy! You can also create other characters such as A~F for hexadecimal. In fact let’s do that now in the following example sketch:

In the function initDisplay() you can see an example of using the instruction then the control byte. In the function clearDisplay() you can see the simplest form of sending digits to the display – we send 0 for each digit to turn off all elements in each digit. The bytes that define the digits 0~9 and A~F are stored in the array digits[]. For example, the digit zero is 63 in decimal, which is B00111111 in binary – which turns on elements a,b,c,d,e and f. Finally, notice the second loop in displayDigits() – 128 is added to each digit value to turn on the decimal point. Before moving on, let’s see it in action:

Our next example revisits the instruction and control byte – we change the brightness of the digits by setting bits 4~6 in the control byte. Each level of brightness is separated into a separate function, and should be self-explanatory. Here is the sketch:

And again, see it in action:

For our final example, there is a function displayInteger(a,b) which can be used to easily display numbers from 0~9999 on the 4-digit display. The parameter a is the number to display, and b is the leading-zero control – zero – off, one – on. The function does some maths on the integet to display and separates the digits for each column, then sends them to the SAA1064 in reverse order. By now you should be able to understand the following sketch:

And the final example in action:

So there you have it – another useful IC that can be used in conjunction with our Arduino systems to make life easier and reduce the required digital output pins.


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, education, I2C, LED, lesson, microcontrollers, SAA1064, tutorialComments (27)

Add a real-time clock to the Freetronics Eleven

Let’s add a DS1307 real-time clock to our Freetronics Arduino-compatible board.

Updated 18/03/2013 – this is also perfect for the Freetronics Eleven board.

Now and again I find myself making another kind of clock or timing device using the Arduino system, and each one has been making use of the Maxim DS1307 real-time clock IC. However every time another clock is being worked on, my DS1307 real-time clock shield needs to come out to play. Although in itself it is a nice shield, at the end of the day – the less you have the better. Originally I used a Freetronics TwentyTen board – which has now been superseded by their Eleven board, however they’re both identical for the purposes of this tutorial.

So what to do? As regular readers will know, my preferred board is the Freetronics Eleven, and within this we have a solution to the following problem:


The Freetronics team have thoughtfully provided a prototyping area in their board – and that will be a perfect home for the real time clock system. Being a cheapskate and a masochist – instead of  following others by using a smaller RTC module I will instead use parts already in stock (except for the battery) and install my own circuit. So, as always – we need a plan. The circuit itself is quite simple, the DS1307 data sheet has a fine example on page thirteen, and here is my interpretation:


So the parts required for our clock circuit will be:

  • IC1 – Maxim DS1307 I2C real-time clock IC
  • 8-pin IC socket
  • R1~R3 – 10k ohm 1% metal film resistors
  • X1 – 32.768 kHz crystal
  • B1 – Panasonic CR1220 3v battery with solder pins (Farnell part number 1298944) [data sheet one and two]
  • One header pin (from those 40-way strips)
  • some thin black single-core wire

The CR1220 battery was chosen over the usual CR2032 due to the smaller diameter. According to the DS1307 data sheet, the battery should last around ten years if it has a capacity of 48 mAh. Our CR1220 is 35 mAh – which will do nicely, perhaps seven years or so. That will have to do. Don’t forget to check the voltage of the battery before installation – it should be just over three volts.

Now to get everything arranged in the prototyping area. When doing this it pays to always have the schematic in front of you as well so you can refer to it when necessary. Planning to use protoboard of any size requires a good plan as well. After spending some time considering component placement, the final layout was as follows:


Each square on the grid represents one hole on the board. After you see the images below, everything will make sense. Before soldering away, it will pay to give the prototyping area a quick clean with some PCB cleaner.

Now it is finally time to get soldering. The first items were the battery, crystal and the resistors. Although the battery was designed to be soldered, I am always a little wary when applying heat to them. Two seconds with the hot iron was enough.

When soldering in the crystal (or anything else), try to keep in mind what the leads will be connecting to. For example, the crystal legs will need to connect to pins 1 and 2 of the IC socket. So bend the crystal leads in the direction of the respective IC socket pins. Doing so will make creating solder joins between them much easier:

The resistors were simple enough. Keep the excess clippings to make jumpers with later. Also notice how the right hand leg of R3 was bent around and brought back up to the top row – this is to help make connections with the 5V rail link:


The next item was the IC socket. Nothing to worry about there, just drop it in and solder away. Don’t forget to bridge the crystal pins to socket pins one and two, and the battery positive pin to IC socket pin three.

Next for the SQW pin. The DS1307 can also output a nice square wave at either 1Hz, 4.096 kHz, 8.192 kHz or 32.768 kHz, with the resulting signal being found on pin 7. It isn’t something really used that often, but you never know. So I soldered in one of these pins, which should make it easy enough to use later on:

Note that if you are using the SQW function, the DS1307 will merrily pulse away once it is set, until the power is cut – the square-wave generator is autonomous to the I2C bus once it has been set. And it remembers (as long as the backup battery is fine). For example, you can upload a sketch to set the SQW to 4.096 kHz, remove power, yank out the ATmega328, power up – and the SQW is still active.

Next we turn the board over, and solder in our jumper wires:


The lead on the top runs from the right-hand side of the pull-up resistors R1~R3 (when facing the top of the board) to the 5V pad. The bottom lead runs from pin four of the IC socket to the GND pad. The negative pin of the battery is also bent over and soldered to the GND pad. Also, connect all the resistors together as shown in the above image (below the TX pin). The next step is turn the board back over and make some more wired connections, the first being pin eight of the IC socket to the resistors and then to the 5V link on the rear:


The next are somewhat longer, they are the leads for the I2C bus. Run a wire from next to IC socket pin six all the way to (and through) the bottom-right hole of the TwentyTen (when facing the top); this will be the SCL line and soldered to analogue 5. Repeat again from IC socket pin five, this is the SDA line (as above) for analogue 4. The joints you have to solder them onto are not that large, however it can be done. Before soldering the wires in, heat up the existing joint to melting point then let it cool again – this makes actually soldering the wire in a lot easier:


And there we have it. At this stage, don’t plug the board in. Do some quality control: check that the soldered joints are complete; check that solder has bridged where you need it, and not where you don’t; use the continuity function (‘beeper’) of a multimeter to spot-check for shorts, and also follow the new 5V and GND lines to ensure they are connected correctly. And finally, insert the DS1307 IC into the socket.


OK – now for some test timing. If you have not worked with the DS1307 IC before, there is a full explanation of how it works within our Arduino tutorials. Here’s a sketch you can use to test the real-time clock. Once you have uploaded that sketch, open the serial monitor box at 9600 bps, and you should have something like this:

Now let’s check the 1 Hz output from the SQW pin:

Recall that you can generate four frequencies with your DS1307, here is an example sketch that does just that:

and here is the result – measured on a freqency counter:

My frequency counter is around twenty-two years old, please be patient with it as the sampling rate is not the best.

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.

Posted in arduino, clocks, ds1307, freetronics, hardware hacking, learning electronics, microcontrollers, tutorialComments (4)

Review – Ikalogic SCANALOGIC2 Logic Analyser/Signal Generator

Hello Readers

Today we will take a first look at the Ikalogic “Scanalogic2” PC-based logic analyser and signal generator. This is a tiny and useful piece of test equipment that should be useful for beginners and experienced engineers alike. It has been developed by two guys in Europe that are dedicated to the craft, and I wish them well. First of all, let’s pull it out of the box and see what we have:


Upon opening the box, one finds a USB cable, the connector leads and the unit itself. It really is small, around 60 x 35 x 20mm. The USB cable is just under 900mm long. Finally a small instruction and welcome postcard which details a quick overview of the software and the unit’s specifications. Ikalogic are to be congratulated for the minimal level of packaging – finally a company that realises one can download the required items instead of printing books, burning DVDs and causing an increase in shipping weight.

The first thing you will need to do is download the latest software. It needs a Windows-based PC with .net framework. Installing took about two minutes, then the ubiquitous system restart. Finally the last preparation is to check for the latest firmware and update it. This is a simple procedure – download a .zip file, extract the .hexe file, then just file>update device firmware in the software. The desktop software checks for new versions before every startup, so you can be sure of having the latest version.

Here are the specifications of the unit from their web page:


Certainly there is a lot there to take advantage of. Personally I consider the logic analyser functions to be of great interest, and will now demonstrate those to see how they can be useful in debugging and generally figuring out what my designs are up to.

One can capture data in two ways, either by using a live sampling mode, or capture mode where you set the device to sample data into its memory, and then reviewing the data using the software. If you are using the live mode, the quality of the sampling will be affected by your PC resources. For example, consider this first demonstration. A very simple Arduino is setting a pin high and low:


In live mode you can still use the horizontal scroll feature to move backwards and forwards through the captured data. One can also expand the data display to the full width of the window. When using the live mode, I found that there was still some variation in the logic levels that was not programmed for. My PC is fairly up to date, consisting of an AMD PhenonII dual-core 3.1 GHz CPU, 2GB RAM at 1066 MHz, running Windows 7 x64. Perhaps I could use some more RAM? A better video chipset? Who knows… Unfortunately I don’t have a more powerful PC to test. Therefore I will stick to the normal capture mode. Doing so is also quite easy – here is the basic setup tab:

It is pretty self-explanatory. If you have a fair idea of your sampling rate, you can drop it down to increase the available sampling time. Here I have selected the lowest sampling rate, as I will just capture the pulses as shown in the earlier demonstration. Once your sample has been collected, you can scroll through it at your leisure, and also save the sample to disk.

In being able to save the data for later retrieval, there are three things that can be done with the data:

  1. As anyone can download the software, you can share your samples by emailing or sharing the files with colleagues – they can playback the sample without owning a Scanalogic themselves, by just using the software;
  2. You can keep the sample for later analysis
  3. You can blast out the captured data using the function generator feature. Neat! Let’s do that now…

Earlier on I captured the following from an Arduino board:


And now I can just right-click on the data (channel one) and select run data generator for this channel then click start on the left. Which results in the following output:

Very good (except for my old CRO). Also notice the log area at the bottom of the application screen – it relays unit status, error messages and so on. Now let’s capture and look at some more interesting sample data. The following example is an example of captured data from an Arduino serial-out pin, which was programmed to send the letter “A” out at 2400 bps using serial.write();


Once you have captured the sample, you can select the parameters of the data stream and decode the sample. As you can see in the image above, the decoder shows the data stream in hexadecimal and the ASCII equivalent.

Next on the test is I2C. This is a common two wire data bus from Philips/NXP, used in many systems. More about I2C with Arduino is here. A very popular example of an I2C IC is the Maxim DS1307 real-time clock. We can use our Scanalogic to eavesdrop on the SCA and SCL data lines to see what is being said between the microcontroller and the DS1307:


So in the example above, the value 0x68 (binary 1101000) is sent down the bus. This is the unique identifier (slave address) for a DS1307 IC. So the Arduino is saying “Hey – DS1307 – wake up”. This is then followed by a 0x00 or directional bit. The DS1307 then replies by sending the time data back to the bus. The first piece of data in the reply is 0x68, which identifies to the I2C bus (recall that 0x68 is the DS1307 identifier) that the data is from the DS1307. Following this is the time and data data in hexadecimal, which is converted to binary-coded decimal in the microcontroller software.

When working with I2C, it really pays to have the data sheet for your IC with you. Then you can decipher the data, direction and timing with the sample data on one side and the timing diagrams on the other. For example, page twelve of the DS1307 data sheet. In doing so, it reminds me how much I dislike I2C 🙂

Moving along. Next we will have a look at some data from the SPI (serial peripheral interface) lines. Again, this is quite simple, you just connect the four hooks into the clock, MOSI, MISO and CS lines, and capture away. The software allows you to select which hook is connected to which line, so you can connect up quickly. At this point I will note that the IC hooks are somewhat inexpensive, and the designers could have spent a few more Euro on including some decent ones. Anyhow, here is the screen dump:


At this point one can realise all sorts of monitoring possibilities. I wish I had one of these years ago when learning digital electronics – you could just monitor the highs and lows over four channels and debug things very quickly. Will keep this in mind when I get around to making a TTL clock.

Anyhow – the Scanalogic2 has a lot going for it in terms of data capturing ability, the price is right, you can update the software and firmware very easily, and the desktop software is freely available in order to share samples with others. There are a few cons though – the IC hooks could be better (I couldn’t connect four in a row onto an IC for the life of me); the unit could use some documentation in terms of a “Getting Started” guide or webpage – so due to this the learning curve is quite high. There is their version here, but I feel it could be expanded upon. Many beginners and amateurs will be attracted to this unit due to the price. However there is a support forum and so on, but answers can vary in quality and time. However, don’t let the cons put you off – this thing is cheap, the software is very good – and it works. Two thumbs up!

To purchase a Scanalogic2, visit the Ikalogic home page. If you need to analyse some data, and don’t want to spend a bucket of money – this is for you.

Posted in ikalogic, product review, review, Scanalogic, test equipmentComments (4)

Getting Started with Arduino! – Chapter Eight

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

In this chapter we will continue to examine the features of the DS1307 real time clock, receive user input in a new way, use that input to control some physical movement, then build a strange analogue clock. So let’s go!

Recall from chapter seven, that the DS1307 is also has an inbuilt square wave generator, which can operate at a frequency of 1Hz. This is an ideal driver for a “seconds” indicator LED. To activate this you only need to send the hexidecimal value 0x10 after setting the date and time parameters when setting the time. Note this in line 70 of the solution for exercise 7.1. This also means you can create 1Hz pulses for timing purposes, an over-engineered blinking LED, or even an old-school countdown timer in conjunction with some CMOS 4017 ICs.

For now, let’s add a “seconds” LED to our clock from Exercise 7.1. The hardware is very simple, just connect a 560 ohm resistor to pin 7 of our DS1307, thence to a normal LED of your choice, thence to ground. Here is the result:

Not that exciting, but it is nice to have a bit more “blinkiness”.

Finally, there is also a need to work with 12-hour time. From the DS1307 data sheet we can see that it can be programmed to operate in this way, however it is easier to just work in 24-hour time, then use mathematics to convert the display to 12-hour time if necessary. The only hardware modification required is the addition of an LED (for example) to indicate whether it is AM or PM. In my example the LED indicates that it is AM.

Exercise 8.1

So now that is your task, convert the results of exercise 7.1 to display 12-hour time, using an LED to indicate AM or PM (or two LEDs, etc…)

Here is my result in video form:

and the sketch.

OK then, that’s enough about time for a while. Let’s learn about another way of accepting user input…

Your computer!

Previously we have used functions like Serial.print() to display data on the serial monitor box in the Arduino IDE. However, we can also use the serial monitor box to give our sketch data. At first this may seem rather pointless, as you would not use an Arduino just to do some maths for you, etc. However – if you are controlling some physical hardware, you now have a very simple way to feed it values, control movements, and so on. So let’s see how this works.

The first thing to know is that the serial input has one of two sources, either the USB port (so we can use the serial monitor in the Arduino IDE) or the serial in/out pins on our Arduino board. These are digital pins 0 and 1. You cannot use these pins for non-serial I/O functions in the same sketch. If you are using an Arduino Mega the pins are different, please see here.  For this chapter, we will use the USB port for our demonstrations.

Next, data is accepted in bytes (remember – 8 bits make a byte!). This is good, as a character (e.g. the letter A) is one byte. Our serial  input has a receiving buffer of 128 bytes. This means a project can receive up to 128 bytes whilst executing a portion of a sketch that does not wait for input. Then when the sketch is ready, it can allow the data to serially flow in from the buffer. You can also flush out the buffer, ready for more input. Just like a … well let’s keep it clean.

Ok, let’s have a look. Here is a sketch that accepts user input from your computer keyboard via the serial monitor box. So once you upload the sketch, open the serial monitor box and type something, then press return or enter. Enter and upload this sketch:


Here is a quick video clip of it in operation:

So now we can have something we already know displayed in front of us. Not so useful. However, what would be useful is converting the keyboard input into values that our Arduino can work with.

Consider this example. It accepts a single integer from the input of serial monitor box, converts it to a number you can use mathematically, and performs an operation on that number. Here is a shot of it in action:


If you are unsure about how it works, follow the sketch using a pen and paper, that is write down a sample number for input, then run through the sketch manually, doing the computations yourself. I often find doing so is a good way of deciphering a complex sketch. Once you have completed that, it is time for…

Exercise 8.2

Create a sketch that accept an angle between 0 and 180, and a time in seconds between 0 and (say) 60. Then it will rotate a servo to that angle and hold it there for the duration, then return it to 0 degrees. For a refresher on servo operation, visit chapter three before you start.

Here is a video clip of my interpretation at work:

So now you have the ability to generate user input with a normal keyboard and a PC. In the future we will examine doing so without the need for a personal computer…

Finally, let’s have some fun by combining two projects from the past into one new exercise.

Exercise 8.3

Create an analogue clock using two servos, in a similar method to our analogue thermometer from chapter three. The user will set the time (hours and minutes) using the serial monitor box.

Here is a photo of my example. I spared no expense on this one…


Here is a video demonstration. First we see the clock being set to 12:59, then the hands moving into position, finally the transition from 12:59 to 1:00.

If you had more servos and some earplugs, a giant day/date/clock display could be made… Nevertheless, we have had another hopefully interesting and educational lecture. Or at least had a laugh. Now onto chapter nine.


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, education, LCD, lesson, microcontrollers, serial monitor, servo, tutorialComments (17)

Getting Started with Arduino! – Chapter Seven

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

Welcome back fellow arduidans!

This week is going to focus around the concept of real time, and how we can work with time to our advantage. (Perhaps working with time to our disadvantage is an oxymoron…) Once we have the ability to use time in our sketches, a whole new world of ideas and projects become possible. From a simple alarm clock, to complex timing automation systems, it can all be done with our Arduino and some brainpower. There is no time to waste, so let’s go!

First of all, there are a few mathematical and variable-type concepts to grasp in order to be able to understand the sketch requirements. It is a bit dry, but I will try and minimise it.

The first of these is binary-coded decimal.

Can you recall from chapter four how binary numbers worked? If not, have a look then come back. Binary coded decimal (or BCD) numbers are similar, but different… each digit is stored in a nibble of data. Remember when working with the 74HC595 shift registers, we sent bytes of data – a nibble is half of a byte. For example:


Below is a short clip of BCD in action – counting from 0 to 9 using LEDs:

However, remember each digit is one nibble, so to express larger numbers, you need more bits. For example, 12 would be 0001 0010; 256 is 0010 0101 0110, etc. Note that two BCD digits make up a byte. For example, the number 56 in BCD is 0101 0110,  which is 2 x 4 bits = 1 byte.

Next, we will need to work with variables that are bytes. Like any other variable, they can be declared easily, for example:

byte seconds = B11111;

B11111 is 31 in base 10, (that is, 2^4+2^3+2^2+2^1+2^0     or    16+8+4+2+1)

However, you can equate an integer into a byte variable. Here is a small sketch demonstrating this. And the result:


If you printed off the results of the sketch in example 7.1, it would make a good cheat sheet for the Binary Quiz program in Chapter Five.

Anyhow, moving forward we now take a look at hexadecimal numbers. ‘Hex’ numbers are base-16, in that 16 digits/characters are used to represent numbers. Can you detect a pattern with the base-x numbers? Binary numbers are base-2, as they use 0 and 1; decimal numbers are base-10, as they use 0 to 9 – and hexadecimal numbers use 0 to 9 then A to F. Run the following sketch to see how they compare with binary and decimal.

Below is a screenshot of the result: the left column is binary, the centre decimal, and the right hexadecimal:


Unfortunately the IC we use for timing uses BCD, so we need to be able to convert to and from BCD to make sense of the timing data. So now we have an understanding of BCD, binary, base-10 decimal, bytes, hexadecimal and nibbles. What a mouthful that was!

Coffee break.

Before we head back to timing, let’s look at a new function: switch… case. Say you needed to examine a variable, and make a decision based on the value of that variable, but there were more than two possible options. You could always use multiple if…then…else if functions, but that can be hard on the eyes. That is where switch… case comes in. It is quite self-explanatory, look at this example:

OK, we’re back. It would seem that this chapter is all numbers and what not, but we are scaffolding our learning to be able to work with an integrated circuit that deals with the time for us. There is one last thing to look at then we can get on with timing things. And that thing is…

The I2C bus.

(There are two ways one could explain this, the simple way, and the detailed way. As this is “Getting Started with Arduino”, I will use the simple method. If you would like more detailed technical information, please read this document: NXP I2C Bus.pdf, or read the detailed website by NXP here)

The I2C bus (also known as “two wire interface”) is the name of a type of interface between devices (integrated circuits) that allows them to communicate, control and share data with each other. (It was invented by Philips in the late 1970s. [Philips spun off their semiconductor division into NXP]).  This interchange of data occurs serially, using only  two wires (ergo two wire interface), one called SDA (serial data) and the other SCL (serial clock).


I2C bus – image from NXP documentation

A device can be a master, or a slave. In our situation, the Arduino is the master, and our time chip is the slave. Each chip on the bus has their own unique “address”, just like your home address, but in binary or in hexadecimal. You use the address in your sketch before communicating with the desired device on the I2C bus. There are many different types of devices that work with the I2C bus, from lighting controllers, analogue<> digital converters, LED drivers, the list is quite large. But the chip of interest to us, is the Maxim DS1307 Serial I2C real-time clock. Let’s have a look:


This amazing little chip, with only a few external components, can keep track of the time in 12-and 24-hour formats, day of week, calendar day, month and year, leap years, and the number of days in a month. Interestingly, it can also generate a square wave at 1Hz, 4kHz, 8kHz, or 32 kHz. For further technical information, here is the DS1307 data sheet.pdf. Note – the DS1307 does not work below 0 degrees Celsius/32 degrees Fahrenheit, if you need to go below freezing, use a DS1307N.

Using the DS1307 with our Arduino board is quite simple, either you can purchase a board with the chip and external circuitry ready to use, or make the circuit yourself. If you are going to do it yourself, here is the circuit diagram for you to follow:


The 3V battery is for backup purposes, a good example to use would be a CR2032 coin cell – however any 3V, long-life source should be fine. If you purchase a DS1307 board, check the battery voltage before using it…. my board kept forgetting the time, until I realised it shipped with a flat battery. The backup battery will not allow the chip to communicate when Vcc has dropped, it only allows the chip to keep time so it is accurate when the supply voltage is restored. Fair enough. The crystal is 32.768 kHz, and easily available. The capacitor is just a standard 0.1uF ceramic.

Now to the software, or working with the DS1307 in our sketches. To enable the I2C bus on Arduino there is the wire library which contains the functions required to communicate with devices connected to our I2C bus. The Arduino pins to use are analogue 4 (data) and analogue 5 (clock). If you are using a Mega, they are 20 (data) and 21 (clock). There are only three things that we need to accomplish: initially setting the time data to the chip; reading the time data back from the chip; and enabling that 1Hz square-wave function (very useful – if you were making an LED clock, you could have a nice blinking LED).

First of all, we need to know the I2C address for our DS1307. It is 0x68 in hexadecimal. Addresses are unique to the device type, not each individual device of the same type.

Next, the DS1307 accepts or returns the timing data in a specific order…

  • seconds (always set seconds to zero, otherwise the oscillator in the DS1307 will stay off)
  • minutes
  • hours
  • day of week (You can set this number to any value between 1 and 7, e.g. 1 is Sunday, then 2 is Monday…)
  • day of month
  • month
  • year
  • control register (optional – used to control the square-wave function frequency and logic level)

… but it only accepts and returns this data in BCD. So – we’re going to need some functions to convert decimal numbers to BCD and vice-versa (unless you want to make a BCD clock …)

However, once again in the interests of trying to keep this simple, I will present you with a boilerplate sketch, with which you can copy and paste the code into your own creations. Please examine this file. Note that this sketch also activates the 1Hz square wave, available on pin 7. Below is a quick video of this square wave on my little oscilloscope:

This week we will look at only using 24-hour time; in the near future we will examine how to use 12-hour (AM/PM) time with the DS1307. Here is a screen capture of the serial output box:


Now that you have the ability to send this time data to the serial output box, you can send it to other devices. For example, let’s make a simple LCD clock. It is very easy to modify our example 7.3 sketch, the only thing to take into account is the available space on the LCD module. To save time I am using the Electronic Brick kit to assemble this example. Below is a short clip of our LCD clock operating:

and here is the sketch. After seeing that clock fire up and work correctly, I felt really great – I hope you did too.

Update – for more information on the DS1307 real-time clock IC, visit this page

Now let’s head back in time, to when digital clocks were all the rage…

Exercise 7.1

Using our Arduino, DS1307 clock chip, and the exact hardware from exercise 6.2 (except for the variable resistor, no need for that) – make a nice simple digital clock. It will only need to show the hours and minutes, unless you wish to add more display hardware. Have fun!

Here is my result, in video form:

and the sketch. Just an interesting note – after you upload your sketch to set the time; comment out the line to set the time, then upload the sketch a second time. Otherwise every time your clock loses power and reboots, it will start from the time defined in the sketch!

As mentioned earlier, the DS1307 has a square-wave output that we can use for various applications. This can be used from pin 7. To control the SQW is very easy – we just set the pointer to the SQW register then a value for the frequency. This is explained in the following sketch:

And here it is in action – we have connected a very old frequency counter to pin 7 of the DS1307:

And there we have it – another useful chapter. Now to move on to Chapter Eight.


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, BCD, ds1307, education, hexadecimal, I2C, LCD, lesson, microcontrollers, tutorialComments (35)

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: