Uploading code to Arduino
It's time to power on the Arduino board and make it do something. In this recipe, we will connect the Arduino to the computer and upload an example sketch from the Arduino IDE.
Getting ready
To execute this recipe, the following are the components required:
- A computer with the Arduino IDE installed
- An Arduino board connected to the computer via USB
How to do it…
Follow these steps:
- Connect the Arduino to the computer using a USB cable. If everything is properly connected, the green LED light will turn on.
- If this is the first time the Arduino has been connected to the computer, driver installation might be required. Please follow the Connecting Arduino recipe to properly set up the Arduino board.
- Start the Arduino IDE and, in the Menu Bar, go to File | Examples | 01. Basics and click on the Blink example. This will load the Blink sketch.
- Make sure your Arduino board is selected in the Board menu. The menu can be found in the Menu bar in Tools | Board.
- We need to check whether the correct serial port is selected. Under Tools | Serial Port, we can see all available serial port devices connected to the computer. On Windows, each port will be labeled as COM followed by a number. Usually, Arduino installs on COM3, but not always. A fast way to check which serial port the Arduino is connected to is to unplug the cable and see which COM port disappears in the menu. That will be our Arduino board. In the Mac, the port should be called something beginning with
/dev/tty.usbmodem
or/dev/tty.usbserial
. - Click on the Upload button on the Tool Bar. If everything runs properly, the TX RX LEDs on the Arduino board will begin blinking for a short time until the upload is done. After this, one LED light on the Arduino Board should slowly blink.
How it works…
When we upload a sketch to the board, the Arduino software first compiles the code. If there is an error in the code, it will write it in the Status Display area and will stop the upload. If no errors are found, it will begin writing the compiled code to the board. Errors will appear if the board or serial port is not properly selected. When everything is correctly set up, the TX RX LEDs will blink, meaning data is being transferred between the computer and the Arduino board. When the transfer is done, the board will reset and the code will immediately begin executing.
The code is stored in the Arduino board until it is erased or replaced by another code. We can take the board and plug it into a battery or to another computer, and it will still execute this blinking.