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 to go.
Traffic lights are POWERFUL! But have you ever wondered how are they built and programmed?
It’s easy to build up traffic lights. I mean.. the software part.
A simple program can simulate that, but the real deal is how can you integrate this into hardware. And that’s exactly what we’re going to demonstrate in this tutorial!
This tutorial is for iPhone developers who want to learn more about how electronics work. You’ll use an open-source micro-controller called the Arduino to make a “traffic light” with red, yellow, and green LEDs. You will then write a program to control the traffic light.
In the process, you’ll get a good grounding in electronics and how the Arduino works. Then in a follow-up tutorial, you’ll see how you can control an Arduino device from your iPhone!
Keep reading to find out more about what an Arduino is, what materials you’ll need for this tutorial, and finally – building your own traffic light!
What is an Arduino?
The Arduino is a popular open-source single-board micro-controller.
Okay wait! Lots of new terms! An Arduino is:
- Open-source: An Arduino can be purchased preassembled, or built by hand with some instructions that are available online. All of the software that runs on the Arduino is open-source (i.e. free!)
- Single-board: The whole thing is on one board that can fit in your hand.
- But what wait a minute: What is a micro-controller?
A micro-controller is a small computer on a single integrated circuit. It contains:
- A processor.
- Programmable input/output peripherals.
This is a lovely picture for the Arduino family that consists of different micro-controllers models.
Why is this something interesting to iOS developers?
- As an iOS developer, you may think that all what you can do with an iOS device is developing an app. Well, you’re wrong!
- Your iOS device can control hardware. You could create an app that turns on the lights, changes the TV channel, or maybe turns off the fan. The possibilities are unlimited.
- One way you can control hardware is with the help of the Arduino! Let’s learn how.
What is this tutorial about?
This is a 2 parts tutorial that aims to give you an overview of how to control hardware through your iPhone.
- The first tutorial shows you how to build up traffic lights with an Arduino Uno (A member of the Arduino family), just to introduce you to the Arduino and how to program it.
- The second tutorial is where things get serious. You’ll learn how to control a LED through your iPhone, with the help of the Arduino Uno & the Ethernet Shield (a module that can be added onto the Arduino Uno to make it connect to the internet).
Let’s start with building up traffic lights.
This is a tutorial on hardware, so to go through this tutorial you will obviously need some hardware to work with :]
You will need the following hardware to proceed with this tutorial:
- Arduino Uno.
- 200 or 330 ohm resistor (x3).
- LED (1 red, 1 amber, 1 green).
You can get most of these materials from any electronics shop. As for the Arduino Uno, here’s a link to their official distributors page.
Note from Ray: When testing out this tutorial, I ended up buying all of my components from Sparkfun Electronics. I bought:
- A Sparkfun Inventor’s Kit. This contains almost everything you need for this tutorial and the next, along with a fun instruction book with other fun projects you can try.
- A green LED (not included in the kit), which you need for this tutorial.
- A Arduino Ethernet Shield which you need for the next tutorial.
In total it ended up costing $141.25. A decent chunk of change, but it provides tons of hours of fun and learning!
Hey! I’m just an iOS developer. What’s with all that hardware?
Ok, don’t panic. I’m gonna give you an overview of all that hardware.
You can skip this part if you already have a background about all that stuff.
A breadboard looks like the picture to the right. It’s just a way to allow you to easily connect wires. You see, it consists of hundreds of holes, where wires can fit in.
Some holes are connected to each others and some are not. The easiest way to explain which holes are connected to which is a picture – so check it out, on the right.
The wires are used to connect the LEDs with the resistors on a breadboard.
But what is a resistor?
You can think of a resistor as a little thing that protects the LED from burning out due to excess electricity by absorbing some of the current that is passing through.
Now we come to the most important piece, which is of course the Arduino Uno.
You can think of it as a small computer that continuously executes a small program as long as it is on.
To turn it on, connect it to your computer with a USB cable, or give it external source of power (9v battery).
You’re gonna write a short “C language” program that simulates a traffic lights system, and then upload it to the Arduino Uno.
For those iOS developers that don’t know how to program in C, don’t worry it’s pretty easy and is very similar to programming in Objective-C.
C is basically objective-C without the Object-Oriented part. So all commands are just the same starting from the datatypes (int, float, char, ..), passing through the conditional statements (if, if else, switch, …), continuing with counter & logic controlled loops such as the (for & while loops), and ending up with functions!
Everything is just the exact same way, so you’re gonna find it so easy to understand.
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.
- The hardware part tells you about the material needed and how to do all the wiring.
- 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:
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:
- 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.
- 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.
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.
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!
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
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.
Where to Go From Here?
Congratulations, you now have hands on experience with creating a simple traffic light with your Arduino Uno! You also have basic knowledge of all electronic related things that you’ve used during the video, such as why is it important to pay attention to the short & long LED legs and why a 200 ohm resistor chosen.
Here is the project with all of the code from this tutorial.
If you want to learn more, check out the official Arduino website. Since it is an open source, so you’ll find a bunch of free tutorials & projects where you can simply copy paste the code and run it into your Arduino. The documentation is also great.
And oh – just one more thing. This was just an introduction to the Arduino Uno to get you started.
The really cool part is the follow-up tutorial, where you’ll learn how to actually control Arduino-baed hardware with your iPhone! It’s super-cool, so don’t miss it! :]
In the meantime, if you have any comments or questions, please join the forum discussion below!
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.