By Terry King
This is the first in a series of articles to introduce the versatile and extraordinary Arduino system to people with no prior knowledge of programming or electronics. We will take you step-by-step through how to set up, program and use the Arduino and provide a series of projects that will help you gain the knowledge you need to free your imagination and work with this revolutionary device.
The instructions make use of what we believe is the best starter kit we have seen on the market and now available through The Shed magazine. The starter kit will take you to intermediate level and leave you proficient to tackle your own more advanced projects. It includes an Arduino clone board, a breadboard, LEDs, cables, sensors, relays and even a servo motor. All you need is imagination.
Low cost
Arduino is a low-cost microcomputer that is the centrepiece of the new toolset of the 21st century. It brings us a new set of capabilities to make things that are active, decision-making, and tireless. The main Arduino toolset is free, open- source software that works on almost any computer. The Arduino microcomputer board designs are open-source and can be freely copied and upgraded. One upgrade is the YourDuinoRobo1 that is included in The Shed magazine Arduino Starter Set. Arduino allows you to make things that are aware of conditions (like temperature, light, etc.) in the real world around it. You decide what decisions it should make and what actions it should take. Actions can include control of lights, motors, valves, heating/cooling, communications and much more. You wire together ideas and electrical things… with today’s equivalent of No 8 Wire.
What can you do with it?
• Home automation to control the energy use of your home;
• Fully automated greenhouses and hydroponic setups;
• Miniature GPS-guided and gyro-controlled aerial drones with cameras;
• Simple things like controlling your workshop lights and fans.
Getting started
All you need to get started is an Arduino-compatible microprocessor board, desktop or laptop computer (for programming the Arduino) and input and output devices. The Shed magazine Arduino Starter Set is an easy low-cost way to get started. It contains everything you will need and more. In fact it will be basis of this series of articles that will get you up and running with the Arduino.
Free Software
Microprocessors like Arduino are just mini-computers on a chip. They need instructions (programs) to carry out their functions. In the Arduino environment we call them sketches. All the software you need to run the Arduino is available from the Arduino IDE (Integrated Development Environment) at www. arduino.cc.
Arduino software looks virtually the same on Windows, Mac or Linux but the installation procedures are different. You will be guided on the different installation procedures from the Arduino IDE.
Check out the YourDuinoRobo1 (much of what we discuss here is also common to other Arduino boards).At the lower left is the PWR LED which should light up whenever the board is plugged into a USB connection, or has power from an external power supply. At the upper left there are two green LEDs which will blink when a software sketch is being downloaded to the board or other data is being transferred over USB. At the top is a red LED which is internally connected to pin 13 through a current-limiting resistor. It should blink after you load the example BLINK software sketch.
Download the IDE for your computer first. Just follow the directions on the Arduino site that apply to your computer. An Arduino is a complete microprocessor system on a board. It includes the processor, power input, and input/output (i/o)pins where data can be input and actions output. These are usually referred to as digital and analogue pins. There are also several LEDs incorporated into the board usually as surface mount devices (SMDs). Plug the Arduino board into your computer via the USB cable provided. If you have the YourDuino plugged in and running, the POWER “ON” LED is on and the “13” LED is blinking. If it isn’t check your connections and start again.
Software
With the Arduino icon on your desktop, click it and you will see the “Arduino IDE Window” pop up. Here you will develop your own software to make Arduino into what you want it to be.
Sketches
The visible text of an Arduino software program is called a Sketch. There are lots of examples that come with the free Arduino software system, and we will look at some of them later, as well as make our own. A Sketch comes alive when you upload it to your Arduino so let’s look at a Sketch, Verifying and Uploading it. Click on FILE which opens a dialogue box. In this you can mouse over EXAMPLES, then across to 1.BASICS and then across to click on BLINK .
The new IDE window opens and explains that BLINK turns on an LED for one second then off for one second and repeats. This is an example of code in the public domain..
Notice that a lot of the text is grey. All of that is just “Comments” for you to read to help understand the Sketch. When you write software, you should also use “Comments” to explain to others what you are doing. (Arduino ignores these comments).
Structure
Every Arduino Program has the same basic structure. SETUP – Runs once at the beginning. LOOP – Runs over and over again, forever. Read the example through carefully, a couple of times. Note the coloured special words that are Instructions. These are unique words that tell Arduino what to do. They have to be spelt perfectly. SETUP: Instructs Arduino about things that need to be done once. Arduino Digital Pins can be used as either INPUT or OUTPUT. You have to tell Arduino when a Pin will be used as an OUTPUT. In this example, there is one line that tells Arduino that Pin 13 must be an OUTPUT. Note the colour of the lettering. The Arduino IDE changes the colour of words as it recognises them as special instructions.
Instructions
When Instructions are two words run together, like pinMode, the beginning of the second word is capitalised. If you change the capital “M” to “m” note that the colour changes to black. If you click VERIFY, you will get an ERROR message. This is because every letter has to be correct and also correct upper or lower case. Change it back. Check the colour. Click Verify again.
VERIFY is a program in your computer that goes through every instruction in your sketch (ignoring “comments”) and checks it against the list of valid instructions. It checks that the structure and sequence of the statements in the sketch are correct. If it’s OK, then it “compiles” or “translates” the sketch into the actual machine code that Arduino really runs on. It saves that ready-to-run code for you to upload to Arduino and run. Other systems would
call this process “Make” or “Compile”. UPLOAD first runs Verify to check and compile your program. Then it communicates to your Arduino over the USB connection and resets the Arduino chip. It talks to software already on Arduino (called the BOOTLOADER ) to load your new program into the Arduino’s memory (Arduino will remember it, even if the power is turned off). Then it restarts Arduino and your program runs the SETUP section and then repeats the LOOP section. Your sketch is running. Let’s look in detail at the instructions. Instruction: digitalWrite
This instruction sets an OUTPUT PIN to either HIGH (connects it to +5 V) or LOW (connects it to GND). Remember: HIGH =1=ON=+5V.So,thefirstlineinLOOP sets PIN 13 to HIGH. There is an LED and resistor already connected to PIN 13, so the LED lights up.
Instruction: delay
The delay instruction just waits for a period of time. The VALUE used with delay is in milliseconds (1/1000 second). So delay(1000); waits for 1000/1000 seconds (1 second).
Notice that every instruction is followed by a semi-colon ” ; “.
Suggestion: Save your own version of BLINK so you can always go back to the original one. Go to File and Save As and call it something like MyBlink. This will go in your SKETCHBOOK where you’ll save your own programs. If things are messed up, just go back to what worked and start again.
1: Pin 13 connected to bread board 2: Connecting the breadboard to the power outlet
Changes
Let’s make a few changes to the sample BLINK program. The LOOP section of your program does all the instructions in the section, and then “loops” back to the top and starts it again, over and over. Note the curly brackets { }. The beginning and end of the section is inside brackets. You will see many sections of bigger programs that are grouped by these brackets.
Let’s change the VALUE in a delay statement to change the way the LED blinks. Think about the four instructions in LOOP.
What’s happening?
Turn the LED on. Wait and look at the LED.
Turn the LED off. Wait and look at the dark.
So, let’s change the ON time to be short. Maybe 50 Milliseconds. That’s 1/20 of a second. Then try 10 milliseconds. The LED is only on 1/100 of the time. Can you still see it blink? How about 1 millisecond? Each time you make a change, click Upload which will first Verify and then Compile your program and send it to Arduino. When you do this the LEDs that are marked “TX” (Transmit) and “RX” (receive) flash as your computer communicates with the Arduino.
Try some combinations of ON and OFF times. Like ON 1000 and OFF 50.
Try making both ON and OFF times shorter and shorter. If you make the ON and OFF times short enough, your eye will no longer see blinking, because of persistence of vision which happens when there are more than about 25 blinks per second. So make the LED be ON for 1/50 of a second and OFF for 1/50 of a second. You can save any of the sketches for use later on. Then go to File>Sketchbook and you’ll see them.
Breadboard
Wires and electronic parts like LEDs and resistors can be plugged into the breadboard and easily be removed or changed. The holes in the breadboard go down into little sockets with metal contacts. Sections of the breadboard have rows or columns that are all connected together, making it easy to have multiple things connected together.
The horizontal rows have five holes (abcde) and (fghij) with sockets that are connected together. Any wires or parts that are plugged into this row are connected together. The vertical columns (+red and -blue) have the same connection running all the way down. We will use these to connect +5V on our YourDuino board to the +Red and to connect GND on our YourDuino board to -Blue. On the breadboard we will mainly use the upper +Red row and the bottom -Blue row. We think of these parallel lines as rails. The top red rail is the +5 Volt rail and the bottom blue rail is the ground rail.
Wires
What about wires? Locatethe “40 pin flat cable” and the “Male-Male Pin Connectors” in
your set. To start we will connect the 5V rail to 5V on the YourDuino board. We will use the CableMaker 40 pin flat cable in the kit for wires. You can easily strip off one or more wires or strip off a section to use as a cable. The ends of these wires are female connectors that can plug onto the connectors on the Robo1 or a Sensor Shield. Your kit has a strip of Male-Male Pin Connectors with 40 pins each. These can be cut or snapped off and used as pins for the breadboard. You can cut or snap off the number of pins you need. To start snap off about 6 single pins. Run a red wire from a +5 pin on the YourDuino to the +red rail on the breadboard. Run a blue wire from a GND pin on the YourDuino to the -blue rail on the breadboard. Now it’s easy to connect things to the 5V (+red) rail or the GND (-blue) rail.
Plug a 220 Ohm (red-red-brown) resistor and a red LED (long pin to the left) into the breadboard as shown. Now add a wire (black is shown) from the same vertical strip as the LED to the GND rail. Connect another wire (green is shown) to the same vertical strip as the left end of the resistor and use a pin to plug it into the YourDuino socket labelled 13. (Use male-male pins where you need a male end to plug into the breadboard or YourDuino)
Power up
Time to power up. Plug the USB cable from your computer into the YourDuino. The PWR LED should come on and the pin 13 LED should be blinking in the way you last programmed it. If necessary load the original Blink and upload it to YourDuino. The LED you just wired up on your breadboard should blink the same as the 13 LED on YourDuino. If not, recheck that you have it wired as in the photos and the LED’s longer lead is to the left. To work out what is happening, unplug the wire from YourDuino pin 13 (keep the pin with it).
Now try two things:
• Plug the free end of the wire into the +5V Rail. It should light up.
• Plug the free end of the wire into the GND Rail. It should be off.
Try it a few times, like 1 second to +, 1 second to GND. Now plug it back into YourDuino Pin 13. It should blink again. What’s happening here? The YourDuino is doing exactly the same thing automatically that you did manually. It is connecting the circuit connected to pin 13 to the +5V Rail and then connecting it to the GND Rail. That’s how digital outputs work.
You have a good beginning in setting up YourDuino, programming it and wiring up external devices.. In our next issue, we’ll think about circuits.
Be sure to check out Arduino Part 2 to find out more!