Your first Arduino program

Screen Shot 2017-08-23 at 8.07.50 AMPrograms in Arduino are know as “sketches”, to create a new sketch go to File –> New. You should now have a blank sketch that looks like the picture on the left. The first part of a sketch is setup. The setup code runs only once at the start, this is where you set up variables, ports, sensors, … The second part of a sketch is loop, the loop code runs continuously.

Screen Shot 2017-08-23 at 8.16.41 AM.pngOn the left you can see an example of a simple sketch, let’s look at all the code … Let’s start with the setup, in setup we are telling Arduino that we are going to use pin 10 as an output, that means that we can tell the Arduino to send information to pin 10. Whenever you want to use a port you have to tell Arduino which ports you want to use and if they will be inputs or outputs.

The first command in loop tells Arduino to turn on port 10 (digitalWrite), what this means is that the port will go from 0V to 5V (HIGH) and if you have an LED connected to this port it will light up. The next command (delay) tells the Arduino to do nothing for 1000 milliseconds or 1 second. Then we turn port 10 off, it will go back to 0V (LOW) and the LED will turn off as well. Lastly we have another wait command. After the wait command the program will go back to the start of loop and the program will start again. The result is an LED that will blink with a 1 second interval.

Things to try:

  • Change the delays and see what happens
  • Connect an LED to port 7 and change the code accordingly
  • Connect 2 LEDs to 2 different ports and make both blink at the same time
  • Create different blinking patterns for both LEDs
Advertisements

Install the Arduino software

arduinoGo to the Arduino website to download the software, look for “Download the Arduino IDE”. Make sure you download the correct version for your Operating System.

Run the installer and open the program. You have to go through the following steps the first time you use your Arduino:

  • Connect the Arduino using a USB cable
  • Select the correct Arduino in the software: Tools –> Board –> Arduino/Genuino Uno
  • Select the correct port: Tools –> Port –> select the USB port

You might have to select the port again if you connect a different Arduino.

 

Some basic Electronics

 

You should have a basic idea of the different components in your bag and what they do. If you’re still looking for information use these websites:

Now you’re ready to start building your own circuits …

breadboard
Breadboard Connections

You need to know how a breadboard works. This image shows how all the holes in the breadboard are connected. Breadboards provide an easy way to build circuits without having to do any soldering.

 

circuit
Basic Circuit

This is an example of a basic electronic circuit. On the left you see the power source, the long line represents + and the short line represents -. If you would connect the + directly to the – you would create a short-circuit and could damage your power source. The resistor “resists” current flowing through the circuit and prevents a short-circuit. More resistance = less current.

In any circuit you need to have an uninterrupted path from + to – before any current can flow.

33631118582_99d191de3d_m
LED Circuit

 

Here is an example of a circuit with an LED. You can see a power source, an LED and a resistor in the circuit. You can also see that there is an uninterrupted path for the current to flow, the current will pass through the LED and through the resistor. You should be able to build this circuit using a breadboard and components.

 

Questions:

  • What will happen if you change the value of the resistor?
  • What will happen if you put the resistor in front of the LED?
  • What will happen if you turn around your LED (switch + and -)?
  • What will happen if you add a second LED after the first one?