In the Arduino world, “blink” is a bit like “hello world” in the programming language world. It is also a great way to test if your Arduino is communicating well with your Arduino IDE, so you can come back to this any time you want to check that you are well connected and communicating well.
These are thus the first things we need to do:
- Download the latest Arduino IDE for your operating system. Arduino IDE is the software editor for your Arduino’s code and the interface to compile and upload your code to the board itself.
- Connect your Arduino board with a USB cable and open Arduino IDE. You should see something like this:
- Make sure your board is selected in the Tools/Board. We are using an Arduino UNO board in this class, so our examples will use it as a reference, but it should be straightforward to adapt.
- Then make sure that you have the proper port selected. It should say something that specifies that you are connected via USB and not for example Bluetooth.
- Then go to File/Examples/01.Basics/blink. Your code should look something like this:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
So, let’s briefly analyze the code:
- Anything that starts with a double slash “//” is a comment and is ignored by the compiler
- Your code is divided into two large sections or functions. setup, which runs once at the beginning and loop that runs over and over after setup. Notice that they are contained in brackets {}.
- Then you have various expressions. Expressions end with a “;”.
- The first expression uses a function called pinMode. If you do a web search for “Arduino pinmode”, it should take you to this reference page for Arduino functions and which will always help you understand what a function does. In this case, pinmode is saying that the built-in LED of your Arduino will be used as an output.
- The second expression uses the function digitalWrite. Again check the reference. We are telling the digital output built-in LED to send a high value or +5v as its output.
- The third expression uses the function delay and it gives it the value 1000, which means that the board will wait one second or one thousand milliseconds until it runs the next expression.
- The fourth and fifth expressions also use digitalWrite and delay to switch the built-in between HIGH and LOW, which generally means +5V and 0V, or ON and OFF. This loop runs forever (or until it runs out of power).
6. The last step is to press the upload button (arrow pointing right) at the top left of the Arduino IDE window. If the upload went well, you should see the message “Done uploading” rather than any errors. Make sure you see the LED blink on and off. If you are using an UNO, the built-in LED is located close to your USB plug and is labeled “L”. It is generally located above “TX” and “RX”.
7. Finally, to make sure you are indeed communicating well, try to modify the value of your delay functions and see how that varies. For example, change one of the 1000 to 100 and you should see a behavior change when you upload, if you change both then blinking should be faster.