## Getting Started with Arduino! – Chapter One

Welcome to Chapter One.

Updated 24/11/2012

I hope you have been enjoying your new-found skills with the Arduino system, and enjoying Massimo’s book. There are many interesting tasks for you to complete in this instalment: finish chapter four and five of the book, which contains some vital information about electricity; we’ll look at a new command or two for your sketches that will save you time and a lot of sketch memory; take a look at pulse width modulation (huh?); go random!; receive inputs from analogue sources; make some decisions; and finally – complete a project as an exercise for you to test your new knowledge.

First of all, please continue on from page 38 until the end of chapter four. This contains excellent instuctions on how to deal with “switch-bounce”, which is vital for future use. See you soon!

Hello again.

Recall from the previous instalment that your exercise involved a lot of repeated commands to light each LED in sequence – for example:

That seemed repetitive and time consuming – the nemesis to the reasoning for the existence of Arduino! However the solution can be found by using the for command. The purpose of the for command is to repeat a section of your sketch a number of times (of course this number is an integer – you cannot repeat a loop 3.141 times!).

Below is a very basic example that blinks an LED on pin 9 five times:

What is happening here is this:

• the integer “wow” is set to have a value of one
• the code in the curly brackets is executed
• the code checks to ensure “wow” is less than or equal to five, then increments “wow” by one

You can also use “wow–” to subtract one from the value of wow, or another variable. Anyway, if wow <=5  the looping continues; and if wow>5 it stops and the sketch moves on.

Hopefully by this stage you have recognised how this can simplify the code for exercise 0.1 from the last instalment. No? Let’s try that now. So instead of that huge block of code to light the LEDS in order up and down, rewrite it to use two loops – one for the up direction, and one for the down.

How did you go? Here’s what we came up with:

Well, wasn’t that better? Those for loops saved us a lot of time, and the use of variables also allows for sketch modifications to be much easier that hunting for each value to change within the sketch.

Groovy. Time for a quickie:

Hey, do you need a random integer? Easy!

random(x) returns a random integer between 0 and x-1. For example, if you need a random number between 0 and 255, use random(256). However, using random() is not entirely random, you need to seed the random number generator inside your Arduino chip. Use randomSeed(analogRead(0)); or another open analogue pin. You can also specify a range, for example random(10,20) will produce a random number between 10 and 19 (the minimum of the range is inclusive, the maximum exclusive – that is why the range is 10~19.

Next on the agenda is pulse-width modulation. Instead of reinventing the wheel, you will now work through chapter five of the book until the end of page 62.

Now that you have emulated a popular cult’s computer, it’s time to have some real fun with PWM and colours. Massimo mentioned about using red, green and blue LEDs to make any colour in the spectrum. This can be done quite easily (like most things) with your Arduino! When you were in school in art classes, you may remember that red + yellow = orange, red + green = blue, and so on. You can achieve the same effect using LEDs, and also vary the brightness between them to create the entire colour spectrum.

First, let’s look at how the primary colours can be used to create all sorts of colours. This example demonstrates briefly the possibilities of experimenting with red, green and blue.

You will need:

• Your standard Arduino setup (computer, cable, Uno or compatible)
• A diffused (not clear plastic) common-cathode RGB light emitting diode. A diffused LED will shine like a light bulb, where a clear one will just look like a 5mm dot.
• 2 50 ohm 0.25 W resistors. They are to reduce the current to protect the green and blue LED section
• 1 150 ohm 0.25W resistor. This is to reduce the current to the red LED section
• a breadboard and some connecting wire
• a camera (optional) – to document your success!

The circuit is quite simple, however the pins of the LED can be tricky. Here is the explanation of their layout:

The resistors are between the PWM output pins and the colour anode of the LED. See the board layout below:

And here is the code… oops sketch:

And here it is in action! Mesmerising…

Wasn’t that fun? I hope you enjoyed that as much as I did writing about it for you. Don’t stare at the LED for too long though… we’re moving on to analogue sensors! Follow the book until the end of page 69. Now for something completely different. It is time to learn about a new command: if…else.

More often than not your sketch will need to make a decision. Is a switch on? Or is it off? If the variable ph equals 8657309 I will send that number to the GSM module to dial the number! Once again, with Arduino – it’s simple.

Example: if the value of temperature is greater than 100, turn off pin 13, otherwise turn it on.

You can also extend this with else if…

Example: if the value of temperature is greater than 100, turn off pin 13; otherwise if value of humidity > 80, turn on pin 7.

With the if…else statement you have a choice of six operators:

• == equals
• > greater than
• < less than
• >= greater than or equal to
• <= less than or equal to
• != not equal to

At this point go and have a break and some fresh air. Because after that, it’s time for…

Exercise 1.1

Now we want to put together all the things learned so far and make something that has analogue inputs, digital outputs, and lots of LEDs… a voltmeter! Imagine a bar graph of ten LEDs, each one represents a voltage range. The range of the voltmeter will be between 0 and 10 volts DC – ideal for testing batteries and cells before they head to the garbage bin.

That sounds like a lot of work (the sketch, not throwing away batteries), but it isn’t when you break it down into smaller tasks. Let’s have a think about it…

We know that analogRead() can measure between 0 and 5 volts, and return an integer between 0 and 1023 relative to the measurement. Ah, but we want to measure up to 10 volts. Easy – use a voltage divider. Use two small resistors of equal value (e.g. 560 ohm 0.25 watt).

Next, how to convert that analogRead() value to represent a voltage we relate to an LED. We know that it will return a value between 0 and 1023, in our case that relates to 0~10 volts. So each LED will relate to one-tenth of the maximum reading. So the first LED will need to be illuminated if the analogRead() returns between 0 and 102.3 (actually 102 as it returns integers, not real numbers). The second LED will need to be illuminated if analogRead() returns between 103 and 205. Etcetera.

Now the rest should be easy… use your new sketch decision-making skills to decide which LED to light up (and don’t forget to turn it off as well) and you’re away…

You will need:

• ten LEDs of your choice. Standard ones are fine, or perhaps a mixture of colours?
• 3 x 560 ohm 0.25 W resistors. One to reduce the current to protect the LED indicator in use, and two for the voltage divider
• 1 x 10k ohm 0.25 W resistor. For use with the analogue input
• a breadboard and some connecting wire
• a camera (optional) – to document your success!

So here is our layout diagram:

And here it is in real life:

… and the action movie! We connected a variable power output to the voltmeter for the sake of the demonstration …

… however due to resistor tolerance and other analogue electrical problems caused by using a breadboard, our voltmeter was a little overenthusiastic. So like any quality piece of test equipment, it required some calibration. There are two ways this could be achieved:

• put a variable resistor in the voltage divider. Then feed a known source, such as 5V from an LM7805 regulator, then adjust the variable resistor until LED 5 was constantly on;
• put another voltmeter (e.g. a multimeter) over the voltage input to measure the voltage being measured by our voltmeter; use the serial.begin and serial.println() functions (from page 69) to send the value of voltage to the screen. Adjust the voltage being measured until you hit 1, 2, … 10V – noting the serial output. Then substitute these values into the if…then decision tree for the LEDs.

The second method is more accurate and easier to accomplish, so I have inserted the serial code into the example sketch below.

Here is a clip showing the results of the second calibration option:

So how did you go? Did it work first time? It’s ok if it didn’t, as you learn by doing and fixing your mistakes. Remember – if you have any questions, leave a comment at the bottom of this post and I will get back to you. But to save you time, here is the sketch. So there you have it. Today you have learned many more useful things to help you on your Arduino journey. And now for Chapter Two.

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.

The following two tabs change content below.

#### John Boxall

Person. Author of http://arduinoworkshop.com Director of http://tronixlabs.com.au Rare updater of http://tronixstuff.com VK3FJBX

### 8 Responses to “Getting Started with Arduino! – Chapter One”

1. Kathy says:

I have tried for so many days to get a larsen scanner going using for loops, I was driving myself insane! I was trying to use arrays and it was a mess. This helped me so much. It really helped me understand what’s going on and what I was doing wrong. THANK YOU!!! This site is such a help when you’re learning on your own!

2. solmaz says:

I really appreciate all the time and effort that you ‘v put into making this useful training. This is wonderful!

3. Alex Roush says:

All of these LED s and wires on Exercise 1.1 (the voltmeter) are giving me a headache. Can you give me a more detailed explanation of how to setup the hardware for this project?

• John Boxall says:

A wire runs from each digital output to the anode pin of the LED, whose cathodes all connect to the 560 ohm resistor which connects to the rest of the circuit.

• Alex Roush says:

Okay, tell me if I have this right so far then: Digital Outputs 2-11 each have wires that connect them to the anodes of all 10 LED s. And then the cathodes of all 10 LED s are connected and then put through a 560 ohm resistor which then runs to ground? Did I do that right so far?

• John Boxall says:

Yes

4. Doug Faltus says:

I get the reference but Jenny won’t answer at 8657309. It’s 8675309. Keep up the great work! Very entertaining writing.

### Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.