Attach your potentiometer’s outer red and black wires to power and ground respectively. Then connect the middle wire from the potentiometer to the Analog input pin 0 on your Arduino board. Check to make sure you’ve connected to the Analog 0 input and not the Digital one that’s on the other side of the board.
Send Serial Output
Use the following Arduino program to send serial output of the values as bytes out to the computer:
int analogPin = 0;
int analogValue = 0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input, divide by 4 to make the range 0-255:
analogValue = analogRead(analogPin);
analogValue = analogValue / 4;
Serial.print(analogValue, BYTE);
// pause for 10 milliseconds:
delay(10);
}
The short delay at the end of the loop prevents your microcontroller from overwhelming the program that will be receiving your data. In practice you’ll find throttling a continuous stream of data to be a useful strategy. Even programs need a moment of silence in which to think.
Check Serial Output
With the Arduino still plugged in via USB to your computer, open the Arduino serial monitor using the right-most button at the top of the Arduino window. By default it will parse serial data at 9600 baud. In this case that’s just what we want.
Turn the potentiometer. You should see a bunch of seemingly random characters scroll by in the serial monitor window. These are the ASCII values associated with the byte numbers being sent from your Arduino board. They aren’t really random. For example, you’ll notice that if you turn the potentiometer very slowly, you’ll get numerals, followed by capital letters and lowercase letters. At the low end of the dial, you won’t see much at all. That’s because the ASCII values from 0 to 31 are non-printing ones like linefeeds or control values for long-obsolete dumb terminals.
Make a Graph
There’s nothing special about the serial monitor in Arduino. Any program on your computer that has access to the serial port can take input from it. This is why serial I/O is so important. It’s a common language which software components can use to communicate, even if they have never heard of each other before. We can demonstrate this by attaching your potentiometer to a very simple graphing program in Processing.
/*
Sensor Graphing Sketch
This sketch takes raw bytes from the serial port at 9600 baud and graphs them.
Created 20 April 2005
Updated 5 August 2008
by Tom Igoe
Updated 2 November 2009
by Rob Faludi
*/
import processing.serial.*;
Serial myPort; // The serial port
int graphXPos = 1; // the horizontal position of the graph:
void setup () {
size(400, 300); // window size
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is usually my Arduino module, so I open Serial.list()[0].
// Open whatever port is the one you're using.
try { // attempt to open this port
myPort = new Serial(this, Serial.list()[0], 9600);
}
// if the port cannot be opened, print an error message, then quit
catch (Exception e) {
println("** Error selecting serial port! **");
println(" Have you attached your Arduino? Does your code specify the right port?");
exit();
}
// set inital background:
background(48,31,65);
}
void draw () {
// nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent (Serial myPort) {
// get the byte:
int inByte = myPort.read();
// print it:
println(inByte);
// set the drawing color. Pick a pretty color:
stroke(123,128,158);
// draw the line:
line(graphXPos, height, graphXPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (graphXPos >= width) {
graphXPos = 0;
// clear the screen:
background(48,31,65);
}
else {
// increment the horizontal position for the next reading:
graphXPos++;
}
}
Troubleshooting
If you get an error message that the serial port could not be opened, then scoll up to check the port listing and edit your code to select the correct port. For example, if your port listing looks like this:
[0] "/dev/tty.Bluetooth-Modem"
[1] "/dev/cu.Bluetooth-Modem"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.usbserial-A4001ufz"
[5] "/dev/cu.usbserial-A4001ufz"
Note the port number that contains the text “usbserial”. This is most likely your Arduino board. In this case it is port 4. Next find the line in your Processing sketch that reads:
myPort = new Serial(this, Serial.list()[0], 9600);
…and change it so that it uses the correct port number, like this:
myPort = new Serial(this, Serial.list()[0], 9600);
Results
Once your graph is up and running, turn the potentiometer and watch the graph react. It should look something like this:
Multiple Sensors
While this very simple setup is fine for sending one sensor value to the computer, it won’t work properly for multiple sensors, where you need to keep track of which one is which. For example, if you were making a driving simulator it would be very important to keep track of which value was your brake pedal and which was your steering wheel! We’ll be learning more about this in class. You can also reference the examples in Chapter 2 of “Making Things Talk.”
Lab-Serial
Parts
Connect a Potentiometer
Send Serial Output
Use the following Arduino program to send serial output of the values as bytes out to the computer:
int analogPin = 0;
int analogValue = 0;
void setup(){
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop(){
// read analog input, divide by 4 to make the range 0-255:
analogValue = analogRead(analogPin);
analogValue = analogValue / 4;
Serial.print(analogValue, BYTE);
// pause for 10 milliseconds:
delay(10);
}
The short delay at the end of the loop prevents your microcontroller from overwhelming the program that will be receiving your data. In practice you’ll find throttling a continuous stream of data to be a useful strategy. Even programs need a moment of silence in which to think.
Check Serial Output
Make a Graph
There’s nothing special about the serial monitor in Arduino. Any program on your computer that has access to the serial port can take input from it. This is why serial I/O is so important. It’s a common language which software components can use to communicate, even if they have never heard of each other before. We can demonstrate this by attaching your potentiometer to a very simple graphing program in Processing.
/*
Sensor Graphing Sketch
This sketch takes raw bytes from the serial port at 9600 baud and graphs them.Created 20 April 2005Updated 5 August 2008
by Tom Igoe
Updated 2 November 2009
by Rob Faludi
*/
import processing.serial.*;Serial myPort; // The serial portint graphXPos = 1; // the horizontal position of the graph:
void setup () {size(400, 300); // window size
// List all the available serial portsprintln(Serial.list());
// I know that the first port in the serial list on my mac
// is usually my Arduino module, so I open Serial.list()[0].
// Open whatever port is the one you're using.
try { // attempt to open this port
myPort = new Serial(this, Serial.list()[0], 9600);
}
// if the port cannot be opened, print an error message, then quit
catch (Exception e) {
println("** Error selecting serial port! **");
println(" Have you attached your Arduino? Does your code specify the right port?");
exit();
}
// set inital background:
background(48,31,65);
}
void draw () {
// nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent (Serial myPort) {// get the byte:
int inByte = myPort.read();
// print it:
println(inByte);
// set the drawing color. Pick a pretty color:
stroke(123,128,158);
// draw the line:
line(graphXPos, height, graphXPos, height - inByte);
// at the edge of the screen, go back to the beginning:if (graphXPos >= width) {
graphXPos = 0;
// clear the screen:
background(48,31,65);
}
else {
// increment the horizontal position for the next reading:
graphXPos++;
}
}
Troubleshooting
If you get an error message that the serial port could not be opened, then scoll up to check the port listing and edit your code to select the correct port. For example, if your port listing looks like this:
[0] "/dev/tty.Bluetooth-Modem"
[1] "/dev/cu.Bluetooth-Modem"
[2] "/dev/tty.Bluetooth-PDA-Sync"
[3] "/dev/cu.Bluetooth-PDA-Sync"
[4] "/dev/tty.usbserial-A4001ufz"
[5] "/dev/cu.usbserial-A4001ufz"
Note the port number that contains the text “usbserial”. This is most likely your Arduino board. In this case it is port 4. Next find the line in your Processing sketch that reads:
myPort = new Serial(this, Serial.list()[0], 9600);
…and change it so that it uses the correct port number, like this:
myPort = new Serial(this, Serial.list()[0], 9600);
Results
Once your graph is up and running, turn the potentiometer and watch the graph react. It should look something like this:
Multiple Sensors
While this very simple setup is fine for sending one sensor value to the computer, it won’t work properly for multiple sensors, where you need to keep track of which one is which. For example, if you were making a driving simulator it would be very important to keep track of which value was your brake pedal and which was your steering wheel! We’ll be learning more about this in class. You can also reference the examples in Chapter 2 of “Making Things Talk.”