Electronics for iPhone Developers Tutorial: Create an Arduino Traffic Light!

This is a post by iOS Tutorial Team Member Dani Arnaout, a Computer Engineering Undergraduate at BAU,Lebanon. Dani has a YouTube channel full of programming & iOS related videos. Every day while going to school, work, or anywhere else you see traffic lights. They are everywhere, and they tell you when to stop and when […] By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Let’s watch the video!

Since connecting all the wires is a very physical task, instead of having a traditional text-based tutorial I thought it would be helpful to make a video showing the process.

The video shows you all the steps needed to create this project. It covers the hardware and the software parts.

  1. The hardware part tells you about the material needed and how to do all the wiring.
  2. The software part guides you through the coding process and how to upload the program that you have written into the Arduino Uno.

So grab your materials and follow along with the video below to make your traffic light! :]

Note from Ray: Here’s how my setup worked after doing this with my Sparkfun Inventor’s Kit:

Layout with Sparkfun Inventor's Kit

Wait – how did that work?

If you’re completely new to electronics, you might still be a little confused as to how this all works. So let’s go over it some more step by step!

We all know electricity as the energy that gives life to objects. It makes your lights shine, powers up your cComputers, and energy companies like to charge you lots of money for using it ;]

Electricity is caused by very small particles called electrons, moving around an electronic circuit.

But what’s an electronic circuit? Well, imagine a battery (power supply) attached with some wires to a light bulb. This makes an electronic circuit. It might look easy, but there are few points to pay attention to:

You’ll find an example of both cases in the adjacent figure:
To the left you can see an open-circuit , the light bulb if off.
To the right you can see a closed-circuit, the light bulb is on.

There’s an important concept here which is voltage.
Technically, voltage is the work needed to move charges between two points.

Batteries are a source of power, LEDs needs a certain voltage to light up.
A LED needs 3 volts, so we need to find a 3v battery for that.

Let’s get back to our Arduino circuit. The USB cable supplies you with +5 volts, but all what you need for a LED is 3 volts, a higher voltage will for sure burn out the LED.

So here comes the resistor, which is used to regulate voltage to protect the LED. Resistors do is to transform this extra energy into heat energy hence getting rid of it.

Note that resistors do not have positive or negative terminals, so you can connect them the way you want.

  1. Closed circuit:

    To get a working electric circuit, you have to ensure it’s closed. Closed circuits mean that all components are attached to each other and no wire or terminal is left alone.

    You’ll find an example of both cases in the adjacent figure:
    To the left you can see an open-circuit , the light bulb if off.
    To the right you can see a closed-circuit, the light bulb is on.

  2. Positive (+ve) and negative (-ve) terminals:

    You’ll notice that the Battery has positive and negative terminals, and so does the LED. But for the LED, instead of having a positive and negative indicators, instead it has loooong (positive) and short (negative) legs.
  3. Voltage:

    There’s an important concept here which is voltage.
    Technically, voltage is the work needed to move charges between two points.
    
Batteries are a source of power, LEDs needs a certain voltage to light up.
    A LED needs 3 volts, so we need to find a 3v battery for that.

  4. Resistors:

    Let’s get back to our Arduino circuit. The USB cable supplies you with +5 volts, but all what you need for a LED is 3 volts, a higher voltage will for sure burn out the LED.

    So here comes the resistor, which is used to regulate voltage to protect the LED. Resistors do is to transform this extra energy into heat energy hence getting rid of it.

    Note that resistors do not have positive or negative terminals, so you can connect them the way you want.

But resistance is measured in Ohm (Ω), so how do you determine what resistor do you need to protect your LED from burning out? Let’s calculate it!

Calculating Resistance

First of all, you need to know that the USB cable will provide you by 5volts, the LED needs only 3, so there are 2 extra volts that we have to get rid of.

There’s this rule called, the Ohm’s law, it is simply U = R x I, where

  • R is the resistance in ohm.
  • I is the current in ampere.
  • U is the voltage in volts.

For now, you don’t have to care about the current as it is 0.01A, and it’s the same for both the LED & the Resistor.

Here’s the mathematical solution for this case. The voltage given by the usb will be distributed for both the LED & the resistor so:

  • U(usb) = U(resistor) + U(LED)
  • U(usb) = R(resistor)I(resistor) + U(LED) [Using Ohm’s Law]

Now Let’s use the values that we already know:

  • 5 = R(0.01) + 3

Solving for R, we’ll get:

  • R(resistor) = 2/(0.01) = 200 ohm.

So we’re gonna choose a 200 ohm resistor for that.

Note from Ray: The Sparkfun inventor’s kit only comes with 330 ohm resistors, but that still works. If you plugin in the equations, you’ll see that you are only sending 1.7volts to the LED with the higher resistor, but that’s still enough to make it light up.

The Resistor of Many Colors

Resistors have different colors, and those colors determine how many ohms are each.
Below is a picture that’ll explain everything for you.

There’s also an app that helps in calculating the resistance according to colors.
It is also a good resource for Arduino libraries, and it’s called Arduino Companion.

The Final Piece

There’s one piece left in the puzzle to discuss. How does the digitalWrite LOW & HIGH work exactly?

These are simply orders to open or close the circuit.

When the pin is LOW then the circuit is opened, hence the voltage crossing the LED is 0v, and the LED is off. On the other hand digitalWrite(2,HIGH); will make sure that the circuit including pin number 2 is fully closed so that the LED will light up.

Contributors

Over 300 content creators. Join our team.