Serial In/Out

Overview

As of version 5.0, the Hub and Widgets support the ability to talk to any device that can send or receive serial values. So if you have a project component the toolkit doesn’t currently support (like an Arduino running custom software, or BeagleBone Black), you can still use it with the rest of the toolkit.

For example, if you have a special kind of device hooked up to an Arduino that requires special code, or you simply want to write you code that moves a servo around in a certain way, you can send or receive data from the toolkit by simply using serial communications. Use the AnalogIn widget to receive data from your Arduino sketch, or AnalogOut to send data to it.

If you need to send multiple values (rather than the single value that AnalogOut sends), try the MultiOut widget.

There’s example code below to get you started.

Serial Communications Basics

  • Connect it to the computer – Be sure you’ve installed any necessary drivers so when you connect the device by USB your computer recognizes the device. If possible, run whatever test programs are available to verify the device and computer can work together.
  • Set the device to communicate at 57600 baud – The AnalogIn and AnalogOut widgets assume the device will communicate with your computer at 57600 baud.
  • Send data from the device to the computer – On the device, have it send values to the serial port connected to the computer. Each time you send a number, it must be terminated by a newline character (“\n”). On the Arduino, this happens automatically when you use the Serial.println() function. Use an AnalogIn, with the controller parameter set to “serial” and the correct serial port name in serialPort.
  • Receive data sent from the computer to the device – On the device, listen to the serial port and every time you get a newline (“\n”), process the serial buffer as a number to use to control other parts of your code. Be sure to get rid of the newline character in your data. Use the AnalogOut widget, and set its “controller” parameter to “serial”. Each value AnalogOut receives from another widget will be send out, terminated by a newline.
  • On Arduino, replace Firmata with your sketch – Normally, the toolkit expects Firmata to be running on the Arduino. But if you want to develop a custom app that uses raw serial communications as explained here, you need to create (or modify) your own Arduino sketch, and load it onto the Arduino using the Arduino software.

Examples

The following examples are very simple versions to show that basics of using serial to communicate. The output sketch checks the value of an analog input on the Arduino and sends that to the computer. The input sketch checks values coming from the computer, and uses them to set the brightness of an LED. You can use these code examples by excerpting this code and including it with your custom code in your own sketch.

Arduino Serial Output Example

This example sketch demonstrates an application running on the Arduino that sends data to the NETLab Toolkit. In the toolkit, use the AnalogIn widget with controller set to serial to receive data from the Arduino.

// periodically read an analog input port and send the value to the serial port
// works with the NETLab Toolkit AnalogIn widget 
// http://www.netlabtoolkit.org/flash/reference/widgets/input-output/analogin/
//

const int   sensorPin = A0;   // select the input pin for the potentiometer
int         sensorValue = 0;  // value used for the analog input

void setup() {
  // set up the serial port
  Serial.begin(57600);
}

void loop() {
  sensorValue = analogRead(sensorPin); 
  Serial.println(sensorValue);
  delay(42); // so we send a value 24 times per second
}

Arduino Serial Input Example

This example sketch demonstrates an application running on the Arduino that receives data from the NETLab Toolkit. Use the AnalogOut widget with controller set to serial to send the data to the Arduino.

// periodically read a value from the serial port, and set a PWM port value
// e.g. to set the brightness of an LED
// works with the NETLab Toolkit AnalogOut widget 
// http://www.netlabtoolkit.org/flash/reference/widgets/input-output/analogout/
//

const int   ledPin = 9; // select the pin for the LED
const char  eol = '\n'; // end of line character

void setup() {
  // set up the serial port
  Serial.begin(57600);
}

void loop() {
  // read the value from the serial input
  // use this value to set a PWM output
  if (Serial.available() > 0) {
    int readValue = Serial.parseInt();
    // read in the line end before we use the value
    if (Serial.read() == eol) {
      // PWM values must be between 0-255
      readValue = constrain(readValue, 0, 255);
      analogWrite(ledPin, readValue);
    }
  }
}

 


Last modified November 24th, 2014