Arduino basics 03 - Upload your code

Make it come to life !

Kurt Schoenhoff

What is a "Sketch"

The Arduino IDE calls the code for an Arduino project a "Sketch", a sketch can be a single file with a few lines of code to multiple files and libraries with thousands (millions?) of lines of code. This is how you give the MCU instructions and information on how you would like it to behave.

It is here we let the controller know how and what pins (if any) we are going to use. If the pin are to be an input or an output, if the pin is to act like a switch or are we going to use it for communications?

We will set conditions to allow it to decide when to act and we also need to describe the required behaviours to the MCU in a language that it can understand.

All of this is accomplished inside the sketch with the help of the inbuilt functions and libraries


Load a sketch

For our first sketch we are not going to write our own, but we will simply use the example "Blink" that comes with the IDE.

The Blink I have supplied has a slight modification to the normal blink that allows it to be used on a variety of devices that may have the builtin led on a different pin.

If you are using a standard Arduino you should be able to use either.

If you are wiring your own please make sure you set the pin you wish to use.




Sketch Blink.ino 

/*
ESP8266 Blink by Simon Peter
Blink the blue LED on the ESP-01 module
This example code is in the public domain
The blue LED on the ESP-01 module is connected to GPIO1
(which is also the TXD pin; so we cannot use Serial.print() at the same time)
Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000); // Wait for two seconds (to demonstrate the active low LED)
}


Odoo image and text block

Compile and upload.

The top left of the Arduino IDE just under the menu system how we compile and upload our code. The green tick is to get the IDE to check over your code for syntax errors and see if it is ready for a compile.

Just next to this is a right facing arrow this is to compile and upload your code to your device. The compiler in the Arduino IDE will parse your code and if it does not find any syntactic errors it will finish compiling and try to upload to your device. 

Unless you have edited the file it should compile correctly and upload to your device, if it does not work have a look at the errors in the bottom black bar and check your device settings. If this does not resolve your problem try looking at the arduino forums after checking their trouble shooting guide or ask us.


What is supposed to happen?

After the Arduino IDE finishes compiling your project it will try to upload it to your board, whilst this happens you may see some leds flickering indicating data is being transferred. Once the program is finished being uploaded your device will reboot, you may hear the device disconnect and reconnect to your USB hub depending on the device. 

What next?

By having a look at the code you compiled have a go at the following.

  • Do you think you can make it blink faster?

  • Do you think you can make the on and off times even?

  • Can you make it flash slow, slow, fast?


Previous

Arduino basics 02 - Connecting

 

Share

Next

Arduino basics 04 - Wire your own led