The first thing to try to get started is a very simple program that doesn't require the breadboard. There is a built-in LED on the Arduino UNO board that is connected to pin 13
. To learn more about the digital pins of the Arduino UNO, there are details on the UNO web page. Select the FAQ
tab and scroll down to the Input and Output section for example to see the following information:
LED: 13. There is a built-in LED driven by digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.
We can make this LED blink with the following code.
The first thing to do is to configure the pin mode as OUTPUT
. The digital pins can be configured to be either input or output. In this case we want to power the LED on and off, so the pin needs to "output" voltage to the LED.
The way to configure a pin mode is with the pinMode
method:
pinMode(pin: 13, mode: OUTPUT)
Once this this set-up is done we can program the main loop to turn the LED on and off. To turn the LED on we turn pin 13 "on" by using this method:
digitalWrite(pin: 13, value: HIGH)
This set the state of the Digital pin 13 to HIGH, which mean high voltage, and so the built-in LED will turn on. To turn it off we use:
digitalWrite(pin: 13, value: LOW)
We need to put some delays in between these two, we do that with the delay
method, which takes a parameter in milliseconds.
delay(milliseconds: 1000)
This will wait 1 sec (1000 milliseconds) before continuing the program's execution.
Our complete program looks like this:
import AVR
// Set pin 13 (built-in LED) mode to output
pinMode(pin: 13, mode: OUTPUT)
while(true) {
digitalWrite(pin: 13, value: HIGH)
delay(milliseconds: 1000)
digitalWrite(pin: 13, value: LOW)
delay(milliseconds: 500)
}
Enter this code in s4a IDE and press the build button (or ⌘B). If everything is ok, plug in the Arduino using the USB cable and press the upload button (or ⌘U). The compiled code will upload to the Arduino UNO (you'll see the TX and RX LEDs light up) and then the built-in LED will blink. It will stay on for 1 second, then turn off for ½ second.
Next, swap the delays in the code so that the LED stays on for only ½ second and turns off for a second. Build and upload your code and see the result.