Archive | relay

Book – “Arduino Workshop – A Hands-On Introduction with 65 Projects”

Over the last few years I’ve been writing a few Arduino tutorials, and during this time many people have mentioned that I should write a book. And now thanks to the team from No Starch Press this recommendation has morphed into my new book – “Arduino Workshop“:


Although there are seemingly endless Arduino tutorials and articles on the Internet, Arduino Workshop offers a nicely edited and curated path for the beginner to learn from and have fun. It’s a hands-on introduction to Arduino with 65 projects – from simple LED use right through to RFID, Internet connection, working with cellular communications, and much more.

Each project is explained in detail, explaining how the hardware an Arduino code works together. The reader doesn’t need any expensive tools or workspaces, and all the parts used are available from almost any electronics retailer. Furthermore all of the projects can be finished without soldering, so it’s safe for readers of all ages.

The editing team and myself have worked hard to make the book perfect for those without any electronics or Arduino experience at all, and it makes a great gift for someone to get them started. After working through the 65 projects the reader will have gained enough knowledge and confidence to create many things – and to continue researching on their own. Or if you’ve been enjoying the results of my thousands of hours of work here at tronixstuff, you can show your appreciation by ordering a copy for yourself or as a gift 🙂

You can review the table of contents, index and download a sample chapter from the Arduino Workshop website.

Arduino Workshop is available from No Starch Press in printed or ebook (PDF, Mobi, and ePub) formats. Ebooks are also included with the printed orders so you can get started immediately.


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.

Posted in arduino, Arduino Workshop, book, books, cellular, clocks, display, distance, ds1307, DS3232, education, EEPROM, freetronics, GPS, graphic, GSM, hardware hacking, I2C, internet, LCD, learning electronics, lesson, no starch press, numeric keypad, part review, product review, projects, RDM630, RDM6300, relay, review, sensor, servo, SMS, time clock, timing, tronixstuff, tutorial, twitter, wireless, xbee13 Comments

Getting Started with Arduino – Chapter Ten

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

In this tutorial we’ll minimise an Arduino Uno board, use time to control – and then find some synergy between the two.

As time goes by, you will want to build projects to use away from the bench, or that are permanent. Although doing so is infinitely fun, it can become expensive if you keep buying Arduino boards for each project or proof-of-concept prototype, or even finished products. However, as always, the Arduino team have solved this problem for us as well. The actual microcontroller integrated circuit (e.g. ATmega328P-PU) can operate with a lot less circuitry than that is contained on your Uno or compatible board. In fact, you can have a functioning Arduino system with three components and a 5V power supply.


To start, please download the Uno schematic from here. Upon glancing at it for the first time, there seems to be a lot of things required. However if you just want to use the digital and analogue pins, you can cut that schematic down to a lot less, however you need to make a couple of changes to how you upload the sketch. Here is what you can get away with:


X1 is the 16 MHz resonator. Using only the main circuit on the left, to upload your sketch you need to place the ATmega chip in your Arduino board, upload the sketch, then very carefully remove the chip and place it into your circuit, breadboard, etc. Please make sure to observe anti-static precautions, and always use a chip puller (below):


Furthermore, if you only have one chip, it would be very wise to have a second or third as a backup. A programmed ATmega with the Arduino bootloader can be had for less than US$6. Below is a shot of my barebones Arduino system at work. It is setup to run this basic example:


The blue and yellow wires run off to a 5 volt power supply. And here is it working:

To recreate this at home, you will need:

  • One ATmega328P-PU microcontroller with Arduino bootrom
  • solderless breadboard
  • 10k ohm resistor
  • 16 MHz resonator
  • (optional – for USB cable) 6-pin header pin
  • (optional – for USB cable) 0.1 uF ceramic capacitor
  • (optional – for USB cable) FTDI cable (the 5 volt version!)

A note about the resonator. Your Arduino board will most likely have a metal crystal, however a resonator is a little cheaper and easier to use, as it has the capacitors in built. If you look at the Arduino schematic, they use a crystal and two 22 picofarad capacitors. So if you use the resonator, pins 1 and 3 go to chip pins 9 and 10, and resonator pin 2 goes to GND. Here is the data sheet for the resonator. They should be easily available, for example from here. The USB cable option will make life a lot easier. The cable is called an FTDI cable, and contains the electronics within to interface between the USB port on your computer and the TX/RX pins on the microcontroller. The wiring details are in the schematic above.

The cable also supplies power whilst programming, or leaving the cable plugged in. Here is my board with the extra wiring connected:


So if you have this kind of setup, you can just plug the cable into the computer USB port and upload sketches as normal. However there are some modifications that may need to be done with your computer’s operating system to make it work. If you are running Linux or MacOS, please visit here; if you are running windows of some sort, go to device manager, right click the USB serial port you are using, select properties, port-settings tab, advanced, and turn on set RTS on Close.

When purchasing an FTDI cable – make sure you get the 5 volt version!

So now you can integrate the Arduino chip into your prototypes much easier and cheaper. However, if you are going to use SPI or I2C devices, more circuitry will be required. We will examine creating these interfaces in more detail later. A good compromise in this situation is a miniature Arduino-compatible board such as the Freetronics LeoStick.

If you are interested in a project using such a barebones setup, please have a look at blinky the clock.


Next on the agenda is a small project to consolidate some previous learning. At the end of chapter nine, we made a (relatively) user friendly clock with an alarm. And in chapter three, we controlled a relay with our arduino. So now we have the ability to create our own on/off timer of which possible uses could be limitless.

In doing so, we can start by modifying the sketch from exercise 9.3. It has the ability to set an alarm time (let’s call it a start time from now on), so we can add the ability to set an end time – it just requires some more variable to store the end time data, and another menu function to set these. Finally, another function is required to check if it is time to turn off (basically identical to the checkalarm() function.

The hardware side of things for the example will be quite simple as well, below is my schematic, and the basic board:





I am using 12v relays as currently that is all I have in stock. The 12V power supply is just an LM7812 regulator from a 20V DC plugpack. For demonstration purposes any low-voltage relay would be fine. A 5V relay would be perfect as you could run it from the Arduino’s 5V rail.

Note: From here on you understand that one can use an Arduino to switch a very high current and/or voltage with some relays. Please exercise care and consult a qualified, licensed person if you are working with mains voltage (100~250V AC… the stuff that comes out of power points/outlets). They can KILL you!

So for this example, I have modified the sketch as described earlier to accept a start time, end time, and display relevant data on the screen. It will switch on and off the relay, which controls a light globe drawing 100mA – 5 times the current an Arduino digital out pin can deliver. It only operates using 24-hour time, to save user confusion. You could control anything as long as you do not exceed the current and voltage maximums for your particular relay.

So here it is in action. The time has already been set previously, so you can watch the setting of the on/off time, and watch it in action. You can skip ahead from 01:03s to 01:48s to save time.

and here is the sketch for your perusal.

As as example the user interface wasn’t that flash, but naturally it can be worked on to be more intuitive. So now it is your chance to do so, with…

Exercise 10.1

Create a timing system for a hypothetical lighting system that controls two independent circuits. Each timer can turn on or off at a specified time, either daily, only on weekdays, only on a certain day of the week (e.g. only Fridays) or only on weekends. The menu system should be easy and quick to use.

For the purpose of the exercise, you can switch on or off an LED to represent each lighting circuit – you already know how to use the relays.

You will need:

  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • Two usual LEDs of your choice
  • 2 x 560 ohm 0.25 W resistors. For use as current limiters between the LEDs and ground
  • 1 x 10k resistor
  • one push button
  • a breadboard and some connecting wire
  • some water
  • DS1307 timer IC circuit components (see this schematic from chapter seven) or a pre-built module
  • one 10k linear potentiometer
  • LCD module as per chapter two

Here are some videos of my interpretation. To save time I have not connected the LEDs, however timer running status is indicated on the second line of the display – an “!” is shown when a circuit has been activated. The first video shows setting the real time, timer data and displaying the results:

and the sketch.

How did you go? With a little more hardware this exercise could become a real product – you could control sprinkler systems instead of lights, thermostats, anything is really possible. Having a larger LCD screen would help with the user interface, perhaps a 20 character by 4 line unit. As long as such a screen has the standard HD44780 interface, you would be fine. Now on to Chapter Eleven


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, 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, barebones, breadboard, education, microcontrollers, relay17 Comments

Getting Started with Arduino! – Chapter Three

This is chapter three of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – in what feels like an endless series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here

[Updated 09/01/2013]

In this chapter we will be looking at relays, creating your own functions, interrupts, and servos.

First on the agenda to day are relays.

What is a relay? Think of it as two things that are very close together: a coil that sometimes has a current passing through it; and a switch, that defaults in one direction. When a current passes through the coil, it generates an electromagnetic field which causes the switch to change state. The beauty of a relay is this: you can use a small current and voltage to switch on and off a very large current and/or voltage – and the switch and coil current are isolated from each other. Here is an example of a relay:


If you look closely you can see the coil and the internals of the switch. This particular model has a double-pole, double throw switch. That means two inputs can be switched in either of two directions. The coil needs 12 volts to activate; and the switch can handle 250 volts AC at 5 amps; and the coil resistance is 200 ohms. Ohm’s law (voltage = current x resistance) tells us the coil current is 60mA. No way you can get that sort of current out of an arduino – hence the need for a relay. (Though if anyone wants to try, stand back and film it for us!) The only problem with relays is that they contain moving parts (the switch) and hence will wear out over time. So if you design a relay into a project, be sure to document a maintenance schedule for the end-user or maintenance people.

How will we use this with our arduino? Once again – very easily. But we need a partner in our relay quest – a switching transistor. A simple, garden-variety NPN model will do, such as the BC548. However, when selecting your transistor you need to ensure that it can handle the current of the relay coil. The data sheet for your transistor (e.g. our BC548) will mention a value called Ic – collector current. For our BC548 this is 100mA, and greater than the 60mA of our relay coil. Perfect!

Almost there… when a coil switches off, all the electromagnetic radiation in the coil needs to go somewhere, otherwise it could pulse down into the transistor. Therefore a simple 1A power diode (such as a 1N4004) is placed across the coil, allowing current to flow through the coil and the diode as a loop until it dissipates.

The last thing to verify is the relay coil voltage. If you have a 5V relay, with a low coil current, you can use the arduino power. However in most cases your coil voltage will be 12V, so it will require its own power supply.

Here is the schematic for what you should end up with, using a 12V relay and a low current transistor. Although it isn’t shown in the schematic, connect the GND from the 12V supply to the Arduino GND.  Since writing this article I have found some 5V relays are available, negating the 12v power supply requirement:


So now for a simple test to get a feel for the operation of a relay… here is our sketch:

Our hardware is exactly as the schematic above. Here is a photo:

And of course a video. Here you can see in detail the coil causing the switch to change poles.

From here on you understand and can use an arduino to switch a very high current and/or voltage. Please exercise care and consult an expert if you are working with mains voltage. It can KILL you.

Creating your own functions…

There are three main components to an arduino sketch: the variables, the structure and the functions. Functions are the commands that request something to happen, for example analogRead();. You can also define your own functions to improve the structure and flow of your sketch. If you have previous programming experience, you may think of these as sub-procedures, or recall using GOSUB in BASIC all those years ago. An ideal candidate for a custom function would be exercise 2.2 from our last instalment – we could have written functions to organise the min/max display or reset the memory. However, first we must learn to walk before we can run…

Do you remember void loop(); ? I hope so – that is the function that defines your sketch after the setup. What it really does is define the code within into a loop, which runs continuously until you reset the arduino or switch it off. You can simply define your own functions using the same method. For example:

Now that function has been defined, when you want to blink the LED on pin 8 three times, just insert void blinkthree(); into the required point in your sketch. But what if you want to change the number of blinks? You can pass a parameter into your function as well. For example:

So to blink that LED 42 times, execute blinkblink(42); Want to specify your own delay? Try this:

So to blink that LED 42 times, with an on/off period of 367 milliseconds – execute blinkblink(42,367);. It is quite simple, don’t you think? Ok, one more. Functions can also function as mathematical functions. For example:

Do you see what happened there? If our sketch determined the radius of a circle and placed it in a variable radius2, the area of the circle could be calculated by:


So now you can create your own functions. Not the most colourful of sections, but it is something we need to know.

Time for a stretch break, go for a walk around for five minutes.


An interrupt is an event that occurs when the state of a digital input pin changes, and causes a specific function to be called and its contents to be executed.

For example, a robot… while it is happily wandering about a sensor is monitoring a proximity sensor pointing to the ground, which changes state when the robot is lifted off the ground and triggers an interrupt – which could switch off the wheels or sound an alarm (“Help, I’m being stolen!”). I am sure your imagination can think of many other things.

So how do we make an interrupt interrupt? It is relatively easy, with a couple of limitations. You can only monitor two pins (for a normal arduino board) or six with an Arduino mega. Furthermore, you cannot use the function delay() in your interrupt function. Now, you need to decide three things: the pin to monitor (digital 2 or 3, referred to as 0 or 1), the function to run when the interrupt occurs, and what sort of behaviour to monitor on the interrupt pin – that is, the change of state of the pin. Your pins also need to be set to output using pinMode();.

There are four changes of state: LOW (when the pin becomes low), CHANGE (when the pin changes state, from high or from low), RISING (when pin goes from low to high) and FALLING (when pin goes from high to LOW). Initially that looks like  a lot to remember, but it is quite simple when you think about it.

Crikey – that was a lot to take in. So instead of theory, it is time for some practice.

Example 3.2 – interrupt demonstration.

In this example, we will have our random number generator from example 2.2, but with two interrupts being monitored. These will be triggered by push buttons for simplicity: (download)

Of course it wouldn’t be right without a photo of the board layout – we have just reused the LCD set-up from example 2.2, and added two standard push-buttons and 10k resistors (as per page 42 of the book) for the interrupts:


And the video… those switches could really have used a de-bounce circuit, but for the purpose of the demonstration they are not really necessary.

Finally – it’s servo time! I really hope you went through the other sections before heading here – although they may not have been that interesting, they will certainly be useful.

What is a servo?

Think of a little electric motor that is connected to a variable resistor. An electric pulse or command is sent to the motor, and it rotates until it reaches a position that matches a value of the potentiometer. Yes, that sounds a little crazy.

A much simpler explanation is this: a servo is an electric motor that can be commanded to rotate to a specific angular position. For example, they are commonly used to control the steering in a remote-control car. Thankfully once again with arduino and friends, using a servo is very easy, and allows your imagination to go nuts, limited only by the amount of spare time and money you have. 🙂

When considering the use of a servo, several factors need to be taken into account. These include:

  • rotational range, that is the angular range it can rotate. 180 degrees, 360 degrees (full rotation), etc.
  • the speed at which it can rotate (usually measured in time per degrees)
  • the torque the servo can generate (how strong it is)
  • the amount of current it draws under load
  • weight, cost, and so on

One of the first things that came to mind was “Wow – how many can I use at once?” And the answer is … 12 on the Duemilanove/Uno, and 48 (wow) on the Arduino Mega. Please note you cannot used analogWrite(); on pins 9 and 10 when  using the servo library. For more details, please see the Arduino servo library page. However you will need to externally power the servos (as we did with the 12V relay) for general use. And as always, check the data sheet for your servo before use.

For our examples and exercises today, I am using the Turnigy TG9. It is quite inexpensive and light, good for demonstration purposes and often used in remote control aircraft.  It can rotate within a range of almost 180 degrees (well it was cheap):


I hope you noticed that there are three wires to the servo. One is for +5V, one is for GND, and one is the control pin – connect to an Arduino digital out. Which colour is which pin may vary, but for this servo, the darkest is GND, the lightest is control, and the middle colour is the +5V. This servo is very small and doesn’t draw much current, so it’s ok to power from your Arduino board. However, if using something larger, or putting your servo under load – as mentioned earlier you will need to run it from a separate power supply that can deliver the required current. If working with more than a couple of these light-duty servos, you should get an external power supply. Moving forward, when working with angles, you should have a protractor handy. Such as:


Now how do we control our servo? First of all, we need to use the servo library. In the same manner as we did with the LCD screen in chapter two, place the following line at the start of your sketch:

So now we have access to a range of servo commands.

Then you need to create the servo object in your sketch, so it can be referred to, such as

Then finally, attach the servo to a digital pin for control (within your void(setup); )

That is the setting up out of the way. Now all you need to do is this…

where pos is a value between 0 and 180. (or more or less, depending on the rotational range of your servo).

Now, the proof is in the pudding so to speak, so here once more is an example of it all coming together and rotating. 🙂 The following example moves from left to middle to right and repeats, to give you an idea of what is happening:

The board layout is quite simple, just the three servo wires to the Arduino board:


And the video. The camera does not record audio, so you cannot hear the cute buzz of the servo.

Ok then – that’s enough reading and watching from your end of the Internet. Now it is time for you to make something; using all the knowledge that we have discussed so far…

Exercise 3.1

We can use our digital world to make something useful and different – an analogue digital thermometer, with the following features:

  • analogue temperature display over a 180-degree scale. The range will vary depending on your home climate. My example will be 0~40 degrees Celsius
  • analogue meter showing whether you can have the heater or air-conditioner on, or off. A rendition of exercise 2.1 in analogue form.
  • minimum and maximum temperature display, displayable on demand, with indicators showing what is being displayed (LEDs would be fine); and a reset button

You will need to combine your own functions, working with temperature sensors; a lot of decision-making, digital and analogue inputs, digital and analogue outputs, and some creativity. To reproduce my example of the exercise, you will need the following:

  • Your standard Arduino setup
  • Water (you need to stay hydrated)
  • Analog Devices TMP36 temperature sensor
  • 2 little push buttons
  • 2 x 10k 0.25 W resistors. They work with the buttons to the arduino
  • a breadboard and some connecting wire
  • two LEDs to indicate minimum/maximum
  • 2 x 390 ohm 0.25 W resistors. They are to reduce the current to protect the LEDs.

So off you go… if you have any questions, leave a comment at the end of the post, or email john at tronixstuff dot com.

And now for the results of the exercise. Here are photos of my board layout:



Here you can see the buttons for displaying minimum/maximum temperature, and the reset button. The LEDs indicate the right servo is display minimum or maximum temperature, or illuminate together to ask if you want to reset the memory. And of course, our video:

And here is the sketch for my example board layout. Congratulations to all those who took part and built something useful. Now to move on to Chapter Four.


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.

Posted in arduino, lesson, microcontrollers, relay, servo, tutorial5 Comments

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: