Arduino008_Serial Output

Serial communication is the process of sending data one bit at one time, sequentially, over a communication channel or computer bus.

The data rate is 9600 bits per second (sometimes called 9600 baud), the receiver will continually read the voltage that the sender is putting out, and every 1/9600th of a second, it will interpret that voltage as a new bit of data. If the voltage is high (+5V in the case of Wiring/Arduino, the PIC, and BX-24), it will interpret that bit of data as a 1. If it is low (0V in the case of Wiring/Arduino, the PIC, and BX-24), it will interpret that bit of data as a 0.

Byte is a code. The BYTE modifier doesn’t format the bytes. It sends out the raw binary value of the byte. The Serial Monitor receives that binary value and assumes it should show you the ASCII (http://www.asciitable.com/) character corresponding to that value. The garbage characters are characters corresponding to the ASCII values the Monitor is receiving.

Also can use Tom Gerhardt’s serial application, Cornflake (http://tomgerhardt.com/Cornflake/).

1 byte = 8 bits

println 255 = begin 2 5 5 end =  5 byte


ASC II charactor

Material Parts – I use potentiometer as output sensor.

Step 1 – import the processing serial library

Download

Processing Library: processing-arduino-0017.zip (Updated 22 Sept. 2009)

Instructions

1. Unzip the library and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook. (You can find the location of your    Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)
2. Run Arduino, open the Examples > Firmata > StandardFirmata sketch, and upload it to the Arduino board.
3. Configure Processing for serial: http://processing.org/reference/libraries/serial/
4. In Processing, open one of the examples that comes with with the Arduino library.
5. Edit the example code to select the correct serial port.
6. Run the example.

import processing library


Step 2 – Graph the sensor’s values

graph the sensor's values



By communicating with Processing, always need to read value in BYTE instead of DEC. Otherwise, the graph will go wrong like the graphing image below:

Failed exercise by reading the value in DEC.

Failed exercise by reading the value in DEC.

Correct exercise by reading the value in BYTE

Correct exercise by reading the value in BYTE

Code in Arduino:

int analogPin = 0;

int analogValue = 0;

void setup() {

Serial.begin(9600);

}

void loop() {

analogValue = analogRead(analogPin);

analogValue = analogValue/4;

Serial.println(analogValue, BYTE);

delay (10);

}

Code in Processing:

import processing.serial.*;
Serial myPort;  //the serial port
int graphXPos = 0;  //the horizontal position of the graph
void setup() {
size (400,300);  //window size
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600); //to open the port number[0]
background (198,232,239); //background color
}
void draw() { //nothing happen in draw becuase it happens in SerialEvent()
}
void serialEvent (Serial myPort) { //every time a new byte arrives int he serial port, serialEvent() called
int inByte = myPort.read(); //get the byte // myPort.read() tells the program to read a byte from the serial port myPort.
println(inByte); //print it
//to draw a graph with byte that reads
stroke(249,111,8); //line color
line(graphXPos, height, graphXPos, height – inByte); // draw the line
//at the edge of the screen, go back to the beginning
if (graphXPos >= width) {
graphXPos = 0;
background(198,232,239); // clear the screen
}
else {
graphXPos++; // increment the horizontal position for the next reading
}
}

import processing.serial.*;

Serial myPort;  //the serial port

int graphXPos = 0;  //the horizontal position of the graph

void setup() {

size (400,300);  //window size

println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600);    //to open the port number[0]

background (198,232,239);    //background color

}

void draw() {     //nothing happen in draw becuase it happens in SerialEvent()

}

void serialEvent (Serial myPort) {     //every time a new byte arrives int he serial port, serialEvent() called

int inByte = myPort.read();     //get the byte // myPort.read() tells the program to read a byte from the serial port myPort.

println(inByte);     //print it


//to draw a graph with byte that reads

stroke(249,111,8);     //line color

line(graphXPos, height, graphXPos, height – inByte);     // draw the line

//at the edge of the screen, go back to the beginning

if (graphXPos >= width) {

graphXPos = 0;

background(198,232,239);     // clear the screen

}

else {

graphXPos++; // increment the horizontal position for the next reading

}

}

3 Responses Subscribe to comments


  1. Tom Igoe

    Not sure why you had to download and install the serial library. It’s one of the core libraries that comes with Processing. And with Arduino.

    Oct 21, 2009 @ 7:33 am


  2. Administrator

    The first time I called out import processing serial, my arduino and processing did not function. That’s why I thought maybe is the import software, so I went to arduino site to search for importing processing. After I fellowed the instruction steps, arduino and processing functioned, I was able to see the correct graphing change by different value input. Thanks for correcting me, otherwise I will keep thinking is because of missing importing steps.

    I found out the problem is I run processing first without uploading the code to arduino.

    Oct 21, 2009 @ 1:12 pm


  3. Ryleigh

    I don’t usually post but I enjoyed your blog a lot.

    Nov 09, 2009 @ 6:38 pm

Reply