Introduction To The Arduino IDE

 

The Arduino IDE has been around for several years now and is used by companies, makers, engineers and hobbyists worldwide on projects ranging from blinking an LED to a mass manufactured commercial product. One of the reasons for it’s success is it reduces the barrier of entry to non engineers to get started with writing code and uploading it to a microcontroller.

This post will look at some example sketches and how to configure the settings for your Arduino board.

Arduino programs are referred to as sketches.

The Bare Minimum Arduino Program:

In the above picture you can see an example sketch called BareMinimum. This sketch has the bare minimum code that is required for the program to compile succesfully. The two main parts of an Arduino sketch are the setup() and loop() functions.

The setup() function only runs the first time the microcontroller wakes up or resets. After that, the program enters the loop() function and loops there forever or until the microcontroller gets turned off or reset.

This setup and loop structure is quite useful as there is normally some things you would like to set up before the main program starts to run. Two common examples of this are configuring certain pins as inputs or outputs or configuring a serial port. Lets take a look at the LED blink sketch to see this in action.

Blink:

To access the Blink sketch, click File > Examples > Basics > Blink

Lets’s take a look at the above code. In the setup function there is only one line of code and that is to set up one of the GPIO pins as a digital output. This line only needs to be run once and the pin will be configured correctly. After that, the program enters the loop() function. This is the main part of the program and you can see that all it does is make the pin high, wait for 1 second (1000 milliseconds) , make the pin low, wait another second. Doing this will make the LED flash.

Let’s upload this program to the Arduino to see it in action.

Configuring The Arduino IDE To Work With Your Arduino:

Before we can upload anything, you’ve got to configure the Arduino IDE to work with your particular type of Arduino.

Click ‘Tools’ to bring up the drop down menu. The main things that need to be set correctly are highlighted in yellow.

  1. Set ‘Board’ to Arduino Nano
  2. Set ‘Processor’ to ATMega328p (old bootloader). If you cant see the (old bootloader part, just select ATMEGA328p)
  3. ‘PORT’ refers to the USB port your Arduino is connected to. On windows, the Arduino is normally the highest numbered port. For example if you have COM1 and COM3, select COM3.
  4. Finally, set  ‘Programmer’ to ‘Arduino as ISP’.

 

With all of that set up, you should be now able to upload programs to the Arduino.

 

Uploading A Program:

Click on the arrow to compile and upload your program to the Arduino.

 

The Serial Monitor:

Sometimes it is useful to have the Arduino send data back to the computer for you to have a look at. For example imagine you have a sensor connected to the Arduino returning a value between 0 and 1023, the easiest way to know what that value is would be to just send it via serial to the computer. You can then open the serial monitor in the Arduino IDE to have  a look at the value and see how it changes over time.

To open the serial monitor click ‘Tools’ > ‘Serial Monitor’.

In order to actually see anything on the Serial Monitor, we first need to have the Arduino send some serial data for us to look at. Copy the code from the sketch below and upload it to your Arduino.

This short program initialises the serial port at a baud rate of 9600. It then prints out the current second  counter over the serial port in an infinite loop.

Once this program has been uploaded to your Arduino, click ‘Tools’ > ‘Serial Monitor’ to open up the serial monitor and look at the data being sent by the Arduino.  It should look something like this:

 

Plotting Serial Data:

If a sensor value changes over time, it can be useful to look at the values in graph form instead of text. The serial monitor function in the Arduino IDE lets you do just that.

The serial monitor is somewhat intelligent, if it sees a number followed by a newline character getting received over serial it will automatically plot it. There is also support for plotting multiple values at once but for now lets just focus on a single value.

The same code from the serial monitor example can be used:

Upload this to your Arduino and click ‘Tools’ > ‘Serial Plotter’.

You should see a blue line being plotted. The scale with automatically adjust as the values change. It should look something like this:

 

 

 

 

 

 

 

 

 

This entry was posted in Tutorial and tagged , , . Bookmark the permalink.

2 Responses to Introduction To The Arduino IDE

  1. Pingback: Adding An LED To The Breadboard And Blinking It | RoboJAM!

  2. Pingback: Blanchardstown Library 3 Day Workshop | RoboJAM!

Leave a Reply

Your email address will not be published. Required fields are marked *