Controlling an Arduino from my computer

Posted in March 2013 by under developer

Last year I was contracted to program a some Siemens LOGO! devices that are now used in a few locations to control water treatment plants. Whilst it was fun to work with logic controllers again, the software is very expensive and the devices are not really suitable for playing with.

At Christmas I was bought an Arduino Uno and last week I finally got around to buying a Starter Kit for Arduino (ARDX) from .:oomlout:.

What I love is that the Arduino lets you interface software and real life objects so simply and for so little money.

To make a start, I have put together a simple proof of concept for controlling LEDs from my netbook over a serial interface.

Here is the breadboard layout

Control Arduino from my computer

You will need to alternate green and red LEDs for r and g to work correctly. Red LEDs should be connected to pins 2,4,6,8 and green LEDs connected to pins 3,5,7,9.

This code will need to be compiled and uploaded using the Arduino IDE. (The source is available on github )

/* leds controlled over serial
 *
 * Richard Holloway
 * https://github.com/richardjh/leds-controlled-over-serial
 *
 * A simple proof of concept that allows you to control LEDs using your computer
 *
 * 0 - turn off all LEDs
 * 1 - turn on all LEDs
 * 2 - turn on LED on pin 2
 * 3 - turn on LED on pin 3
 * 4 - turn on LED on pin 4
 * 5 - turn on LED on pin 5
 * 6 - turn on LED on pin 6
 * 7 - turn on LED on pin 7
 * 8 - turn on LED on pin 8
 * 9 - turn on LED on pin 9
 * g - turn on all green LEDs
 * r - turn on all red LEDS
 *
 * There is also a physical button that turns on all LEDS on pin 10
 * and the RESET button will turn off all LEDs as it resets the program
 */

String inChar = "";
int    pin;
int    val = 0;
int    button = 0;
 
// Setup runs once when the program starts
void setup() {
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
 
  pinMode(10,INPUT);
}
 
// Loop runs over and over
void loop() {
   button = digitalRead(10);
   if ( button == 0 ) {       // Button has been pressed, turn on all LEDs
      digitalWrite(2,HIGH);
      digitalWrite(3,HIGH);
      digitalWrite(4,HIGH);
      digitalWrite(5,HIGH);
      digitalWrite(6,HIGH);
      digitalWrite(7,HIGH);
      digitalWrite(8,HIGH);
      digitalWrite(9,HIGH);
    }
}
 
// SerialEvent is run each time loop runs and data is received on RX
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
 
    if ( inChar >= 50 && inChar <= 57 ) { // 50 is ascii for 2, 57 is 9
      pin = inChar - 48;                  // Convert ascii to pin number
      switchPin();                        // Toggle state of that pin
    }
 
    if ( inChar == 49 ) { // 49 is 1, all LEDS on
      digitalWrite(2,HIGH);
      digitalWrite(3,HIGH);
      digitalWrite(4,HIGH);
      digitalWrite(5,HIGH);
      digitalWrite(6,HIGH);
      digitalWrite(7,HIGH);
      digitalWrite(8,HIGH);
      digitalWrite(9,HIGH);
    }
 
    if ( inChar == 48 ) { // 48 is 0, all LEDS off
      digitalWrite(2,LOW);
      digitalWrite(3,LOW);
      digitalWrite(4,LOW);
      digitalWrite(5,LOW);
      digitalWrite(6,LOW);
      digitalWrite(7,LOW);
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
    }
 
    if ( inChar == 114 ) { // 114 is r, all red LEDs on, all green off
      digitalWrite(2,HIGH);
      digitalWrite(3,LOW);
      digitalWrite(4,HIGH);
      digitalWrite(5,LOW);
      digitalWrite(6,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(9,LOW);
    }
 
    if ( inChar == 103 ) { // 103 is g, all green LEDs on, all red off
      digitalWrite(2,LOW);
      digitalWrite(3,HIGH);
      digitalWrite(4,LOW);
      digitalWrite(5,HIGH);
      digitalWrite(6,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(9,HIGH);
    }
 
  }
}
 
// my own function to toggle state on a pin
void switchPin() {
  val = digitalRead(pin);
 
  if ( val == LOW ) {
    digitalWrite(pin,HIGH);
  } else {
    digitalWrite(pin,LOW);
  }
}
            

Then to connect your computer to the device, first connect the serial cable and then use a terminal client such as screen to make the connection.

screen /dev/ttyACM0 9600
            

See the screen man page for more options.

If you have problems connecting, make sure your user is in the `dialout` group.

The source code and source for the diagram are available on github.