Introduction: This project introduces you to the wonderful world of Arduino and how to program physical devices. In this lesson you will learn how to write code and make an LED flash. Goals 1 - Get to know your Arduino board and learn about some of its components. 2 - Write code to make an LED flash using the Arduino software. 3 - Upload the code you have written to your Arduino board. 4 - Challenge your skills! - Follow these Instructions one by one Test your project - Click on this to Test your project Save Save your project - Click on this to Save your work Page 1 - Lesson 3: Arduino
Step 1: Meet the Arduino What is an Arduino? An Arduino is a microcontroller that can be used to make interactive projects that are connected to switches and sensor, controlling lights, motors and speakers or even talk to a computer. They are brilliant for making things that communicate with the physical world. We are going to use it as the brains of our DIY Gamer, using the soldered buttons as our inputs. By the end of the term, you will have written a whole game for it, but today we are just going to write some simple code to get an LED flashing. First lets get to know the Arduino board. Power Pins Separate your DIY Gamer Holding the front firmly with one hand, and the back with the other, gently pull the back off the DIY Gamer. This separates the front with PCB from the back with the Arduino. Pick up the Arduino half and have a good close look. Analog Pins Microcontrolle r Chip Digital Pins Power Input Clock USB Connection 1. Can you find the Micro controller? This is the brains of the board. It talks to everything else gathering information and giving out instructions. 2. Can you find the Clock? This ticks at 16MHz. This means it tells the microprocessor to talk to the rest of the board 16 million times a second. That s a lot. 3. Can you find the USB connection? This allows you to connect the Arduino to your computer and not only transfers the code, but supplies the power. 4. Can you find the Digital PINS 0 13? Using code, these can be turned on or off or can read things that are on or off, or true or false, like a light or a motor. 5. Can you find the Analogue PINS A0 A5? These can be connected to read variable inputs like brightness, volume, temperature and pressure. Page 2 - Lesson 3: Arduino
Step 2: Write the flashing LED code 1. Plug in the LED Pick up an LED, You ll notice it has one leg longer then the other. In electronics when something is different about a component it is trying to tell us something like it has to go a certain way round or it won t work. The long leg is called an Anode, this is the positive leg which means it needs power. The short leg is called a cathode and this is the negative leg. Insert the long leg into pin13 and the short leg into the GND (ground) pin next to it on the Arduino. Long leg = + Short leg = - It should open a new blank file called sketch. 3. Write the set up code Every Arduino sketch needs a setup code that will be run once at the beginning. Type in this code to tell the Arduino that we are using PIN 13 as an output, and that we will be putting an LED in it. It will automatically colour itself if typed correctly. Page 3 - Lesson 3: Arduino
Step 2: Write the flashing LED code 4. Write a comment Comments are for us to read not the Arduino. They help to label your code making it easy for you and others to understand. Type // before text and the Arduino will ignore it and not include it in the compiled code. Add the following comments to your code. It should automatically be in grey. 4. Write the void loop function Once Set up is complete, it runs the first loop code. It will keep running this loop again, and again and again until you tell it to stop or remove the power. Add the void loop code shown in the box to: Tell pin13 to have high power (high = on) Do this for 1000 milliseconds (1 second) Tell pin13 to have low power (low = off) Do this for 250 milliseconds (1/4 of a second) Altogether, this is telling the led to turn on for 1 second and then off for a ¼ second. Again and again and again in a loop. Page 4 - Lesson 3: Arduino
Test your project Click the tick that looks like this to verify your project and compile the code. Mistakes Code is very fussy and likes everything to be just so. When you compile, any mistakes are listed in the bottom bar in red. Missed a capital letter? Missed semi-colon? Missed a curley bracket? It will be there somewhere. Treat is as a spot the difference game. When you see Done Compiling in the status bar, you are now ready to upload it to the Arduino. Save Save your project Click the arrow facing downwards that looks like this to save your code. Things to try Add comments to help remember what the code does. Page 5 - Lesson 3: Arduino
Step 3: Upload to the Arduino 1. Plug the USB cable from the laptop into the Arduino The board should now have the tiny green on LED lit to show it is getting power. 2. Select the board Go to the tools menu and select the board, Arduino Uno 3. Select the serial port Go to the menu Tools > Serial Port and select the port that the Arduino is connected to. This name should be something like / dev/tty.usbmodem on a mac, or COM3 or higher on a PC. To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port. Page 6 - Lesson 3: Arduino
Test your project Click the green arrow to transfer your code to the Arduino. Wait a few seconds, you should see the RX and TX LEDS on the Arduino board flashing. If the upload is successful, the message Done uploading. will appear in the status bar. Does the led in PIN 13 start to flash on for 1 second, then off for a ¼ of a second? If it does, well done! You ve just written your first Arduino sketch. Challenge: Change the speed Can you make it flash slower? Can you make it flash faster? Can you make it flash a word in Morse code? Well done, you ve finished. Why don t you swap Arduino Morse code with a friend? Page 7 - Lesson 3: Arduino