Update to Arduino Programming Course

The Arduino programming course originally started in 2014 is being updated and extended. Updates include using the newest version of the Arduino IDE and updating the videos in the course to HD video.

Currently parts 1 to 5 of the course have been updated which cover Arduino sketch structure and flow, Arduino main loop, calling functions, variables, arithmetic operators and relational operators.

Once updates have been completed the course will be extended to include new material and topics. Take a look at the Arduino programming course contents page to see the currently available tutorial parts of the course.

 

Atmel Software Framework Tutorial

The final part of the Atmel Software Framework tutorial is now available. It covers how to use the ASF help documentation and how to find and use example ASF code. The tutorial uses an Atmel ARM Cortex on an Atmel Xplained evaluation board.

A previous blog post listed the current articles in the tutorial at the time of posting. The tutorial series is now complete.

ASF – Atmel Software Framework Tutorial

The first part of the ASF tutorial series introduces ASF and shows the basic structure of ASF. Each part of the tutorial series explains various parts and usage of the ASF in easy to follow steps.

The final part of this series shows how to use the ASF documentation and ASF example projects in Atmel Studio 7.

 

Installing Arduino Software and Drivers in Windows 10

There are some choices to be made when installing Arduino software and drivers in Windows 10. Arduino offer two ways of installing the Arduino IDE on a Windows PC — Windows installer file and a Windows zip file. Windows 10 will also install a default driver when an Arduino is plugged into the USB port of a PC. This driver can be replaced by the Arduino driver that comes with the Arduino software.

Arduino Software and Drivers

Installing Arduino software using the Windows zip file is the simplest method of installing the software. It is just a matter of copying the folder out of the downloaded zip file into the desired location on the PC. This allows the software to easily be removed by deleting the folder.

When Windows 10 installs a driver automatically, the Arduino just looks like a COM port in Windows Device Manager. It is not recognized as an Arduino, although it will work properly and code from the Arduino IDE can be loaded to it. After installing the Arduino driver that comes with the Arduino software, the Arduino will be shown as an Arduino on a COM port in Device Manager. The difference is shown in the image below.

Windows Default COM Driver and Arduino Driver

Windows Default COM Driver and Arduino Driver

As can be seen in the image, with the default Windows 10 driver installed, the Arduino appears as USB Serial Device (COM4) (left), but after the Arduino driver is installed the same Arduino appears as Arduino Uno (COM4) (right).

Full instructions on installing the Arduino IDE software and updating the Arduino driver can be found in the article on how to install Arduino software and drivers on Windows 10.

Processing Serial Port Number Programming and Selecting

Processing language applications that use the serial port to connect to serial devices such as Arduino need to use the correct serial port number that the serial device is connected to. Here are three methods for selecting and connecting to a serial device from a Processing application, namely, hard-coding, auto-detection and user selected Processing serial port.

Hard-coding a Processing Serial Port Number

Hard-coding the serial port number in the Processing application is the default way used in most Processing examples. In the library reference documentation for the Serial library, the hard-coded method is used.

Listing the Serial Ports and Connecting

Hard-coding examples usually print the list of serial ports on the computer to the text area at the bottom of the Processing IDE using the following line of code:

println(Serial.list());

This allows the programmer to determine which number the desired port is in the list of ports. The ports listed in the text area of the IDE are numbered from 0 in the list, so in the list of serial ports in the image below from a Linux computer, /dev/ttyACM0 would be 0 in the list, /dev/ttyS0 would be 1 in the list, etc.

Processing Serial Port List

Processing Serial Port List

 

 

 

 

 

On a Windows computer, these would typically be COM1, COM2, COM3, etc. which are also numbered in the list starting from 0.

After the correct serial port name from the list is found and the number of the serial port in the list has been determined, the serial port can be connected to in the Processing application code.

// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;       

// List all the available serial ports:
println(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
  
// Send a capital A out the serial port:
myPort.write(65);

The above code is taken from the Processing Serial library documentation and first lists the available serial ports on the computer, then connects to the first serial port in the list. The following line of code is where the serial port number from the list is hard-coded in the application:

myPort = new Serial(this, Serial.list()[0], 9600);

The parameter Serial.list()[0] connects to the first serial port in the list of serial ports. In the above image, this would be /dev/ttyACM0. On my Windows PC, there is a default COM port, COM3. When an Arduino is plugged into the PC, it appears as COM4 or COM5, depending on which USB port the Arduino is plugged into. Processing code for the Windows PC must use Serial.list()[1] to connect to the second serial port on the PC which would be either COM4 or COM5 in this particular case.

Disadvantages of Hard-coding the Serial Port Number

If a Processing application that connects to a serial port is run on a different computer, or more than one serial device is connected to a computer, the desired port to connect to may change position in the list of ports. It would then be necessary to modify the Processing code to select the correct serial port. This means that the Processing IDE would need to be loaded onto every computer that the application is run on, in case the serial port number changes.

Auto-detecting a Processing Serial Port Number

A USB serial port can be auto-detected by a Processing application by starting with the USB serial device unplugged and then starting the Processing application. The Processing application can be programmed to store the list of serial ports before the desired USB serial device is plugged into the computer. When the device is plugged in, the Processing application can detect it and find it’s serial port name and number.

Code from the Processing application that auto-detects which serial port number an Arduino is plugged into uses the USB serial port auto-detect method and can be used as a starting point for any Processing project that is to use serial port auto-detect.

User Selected Processing Serial Port Number

Window controls in a Processing application can be used so that a user can scroll through the list of available serial ports and connect to the desired port. This method has the advantage that if the application is to run on different computers, the correct serial port can be selected by the user at run-time.

The image below shows controls in a Processing application window used to select and connect to a serial port.

Processing Serial Port Select Using Window Controls

Processing Serial Port Select Using Window Controls

Up and down buttons allow the list of serial ports to be scrolled through to select the desired port. When the Connect button is clicked, the application will connect to the selected (currently displayed) serial port.

A Disconnect button allows disconnecting from the serial port, so that the application can connect to a different serial port.

The Refresh button updates the list of available serial ports, which is useful in cases where a serial device is connected to the PC after the application is started. The new device will be added to the list when the refresh button is clicked.

A Simple Button for Processing Language Code

A button that can be placed in the application window of a Processing program (processing.org). The button code allows the button to be drawn and checked to see if it has been clicked by a mouse.

Processing button used in window of application

Buttons in a Procesing Application

At this point in time Processing does not have any button controls as part of the language or built in libraries. The code below is a class for a very simple button that should be easy for beginners in programming to understand, study and improve on. An example of using the button follows.

The image shows two buttons created in a Processing application window using the Button class.

Processing Button Class

This button class can be copied to any Processing sketch and then used in the sketch as demonstrated by the example that follows.

class Button {
  String label;
  float x;    // top left corner x position
  float y;    // top left corner y position
  float w;    // width of button
  float h;    // height of button
  
  Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
  
  boolean MouseIsOver() {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
      return true;
    }
    return false;
  }
}

Processing Button Demonstration Example

This application uses the above Button class to create a button on the screen. When the mouse cursor is moved over the button, a square is drawn in the window, when the button is clicked, text is written to the text area console of the Processing IDE. This video shows the example code running:

// Processing application that demonstrates the Button class by creating a button
// Draws a square in the window when the mouse cursor is over the button
// Writes to the Processing IDE console pane when the button is clicked
// 3 July 2015    http://startingelectronics.org
Button on_button;  // the button
int clk = 1;       // number of times the button is clicked

void setup() {
  size (300, 150);
  smooth();
  
  // create the button object
  on_button = new Button("Click Me", 20, 20, 100, 50);
}

void draw() {
  // draw a square if the mouse curser is over the button
  if (on_button.MouseIsOver()) {
    rect(200, 20, 50, 50);
  }
  else {
    // hide the square if the mouse cursor is not over the button
    background(0);
  }
  // draw the button in the window
  on_button.Draw();
}

// mouse button clicked
void mousePressed()
{
  if (on_button.MouseIsOver()) {
    // print some text to the console pane if the button is clicked
    print("Clicked: ");
    println(clk++);
  }
}

// the Button class
class Button {
  String label; // button label
  float x;      // top left corner x position
  float y;      // top left corner y position
  float w;      // width of button
  float h;      // height of button
  
  // constructor
  Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
  
  boolean MouseIsOver() {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
      return true;
    }
    return false;
  }
}