The First Steps
Once you open the Arduino software you will find a screen that looks like this:
To get started complete the following steps:
- Make the text bigger - Click File> Preferences and change the value (to 20, for example) in the "Editor Font Size" box
- Click Tools> Board and ensure Arduino/Genuino Uno is selected, or Arduino Mega depending on which board you are using.
- Click Tools> Port and select the COM Port showing the Arduino board.
- Select all the existing text in the editor, and delete it by clicking the backspace key on the keyboard.
- Copy the following text below, and paste it into the Arduino Software IDE. If you have done this successfully, you will notice that keywords will change colour. This is called syntax highlighting.
__________________________________________________________________________
int button = 3; //attach a button to digital pin 3
int LED = 7; //attach an LED to digital pin 7
void setup()
{
pinMode(button, INPUT); //define button an INPUT device
pinMode(LED, OUTPUT); //define LED an OUTPUT device
}
void loop()
{
int buttonState = digitalRead(button); //read the status of the button
if(buttonState == 1)
digitalWrite(LED,HIGH); //apply high voltage to LED
else
digitalWrite(LED,LOW); //apply low voltage to LED
}
_________________________________________________________________________
- Connect one LED to Pin 7, and one push button to Pin 3 on the board.
- Click the Verify button (the tick icon, top left), and ensure there are no errors. The sketch will need to be saved before it will be verified - simply save this to the computer or a USB if you wish to keep this code.
- Once it has been verified with no errors, upload the sketch using the Upload button (arrow icon).
Once uploaded, the LED should illuminate when the button is pressed, and stop illuminating when the button is released.
So what's going on?
Lines 1-2
Think of these lines (anything before the "void setup()" on line 4 above) as a bit of a dictionary. This is where variables are defined. In the above examples, 2 variables, both of int type (integers - that is, whole numbers) were defined. One was called "LED" and one was called "button". The variable "button" was assigned the value of 3 (which will later be linked to pin 3 on the hardware), while the variable "LED" was assigned the value of 7.
Lines 4-8
This is the void setup() section. The lines of code between the open and closed braces (aka curly brackets) is run only once. Think of this section as getting everything setup in a kitchen when baking a cake - you only need to preheat the oven once, you only get the milk out of the fridge once, and you only open the recipe book once.
The pinMode part of lines 6 and 7 tells Arduino that the information contained within the sets of parentheses (aka round brackets) refers to pins on the Arduino board. The phrase pinMode(LED,OUTPUT) says to Arduino, that the variable, with the value of 7, refers to Pin 7, and is an OUTPUT type of pin. This means that Arduino will send a signal out of this pin, rather than waiting for a stimulus to come into the pin.
Lines 10-17
This is the void loop() section. The lines of code between the open and closed braces repeat, in order from the top to the bottom, until there is code specifying it to stop, or until there is no power provided to keep running.
Line 12 defines a new variable, called "buttonState", and is of integer type. The value of this variable is assigned each time the loop repeats, and it is the value associated with the state of the "button" pin. That is, the line looks at Pin 3, the "button" pin, and reads the state. If it is not pressed, it has a value of 0, and if it is pressed, it has a value of 1.
The next part of code is an "if" loop. If the condition within the parentheses is true, the code following is executed. If not, Arduino passes over the code following and enters the "else" section.
In this case, if the value of the variable "buttonState" is equal to 1 (the symbols == means equals, whereas in Arduino, using the equals symbol (=) assigns a value to a variable), the condition is true and the code following runs. That code "writes" (or sends) a signal to the variable LED (which has a value of 7, and because LED is an OUTPUT in pinMode, it means Pin 7) of HIGH. HIGH simply means maximum voltage for the pin (i.e. 3.5 or 5V in most cases). LOW means minimum voltage for the pin (i.e. 0v).
If the condition was true, Arduino passes over the else statement and reaches the closed brace. This is the signal to loop once again, and start the buttonState checks again.
If there is more than 1 line of code needed within an If (or If...else) statement, braces can be used to contain multiple lines of code which relate to the "If" and "else" segments of code. "else" is not always required, if the "else" is simply a "do nothing" function. If the code required a green or red LED to be turned on for a given condition, the else would be required (it would specify which LED to turn on).
"If" statements can be combined with else statements, to create an "else if (xxx)" condition - this can be useful for specifying 3 or more outcomes based on a set of inputs (e.g. using a rotary angle sensor, and wanting different outputs after reading values of 0-10, 11-20, 21-30, etc.)