Framework (A-Z)

Reference for Wiring version 1.0 Build 0100+ If you have a previous version, use the reference included with your software. If see any errors or have any comments, let us know.



int val;

void setup() {
  Serial.begin(9600);  // Start serial Serial at 9600 baud

void loop() {
  if ( Serial.available() > 0 )  // if data is available to read
    val =;           // read it and store it in 'val'
  analogWrite(0, val);
Description The Wiring Serial serial port allows for easily reading and writing data to and from external devices. It allows two machines to communicate and gives you the flexibility to make your own devices and use them as the input or output to your Wiring programs. The serial port used to be a nine pin I/O port that existed on most PCs and can be emulated through a USB serial adapter. The Wiring I/O board has two built-in (hardware) serial ports called Serial and Serial1. The port Serial is available through the USB connector in the Wiring hardware. The port Serial1 is available on Wiring I/O pins 2(Rx) and 3(Tx). It is possible to access the serial port Serial through pins. On the Wiring v1 boards it is available on pins 32(Rx) and 33(Tx). On the Wiring S board it is available on pins 0(Rx) and 1(Tx). Typical baudrates are: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600 and 115200. Note: depending on the hardware it might be possible to have Serial2 and Serial3 available. Using the Serial Port All Platforms: If you're using a program to check if the serial port is working, then it won't be available to Wiring. That means that if you're using HyperTerminal or whatever to see if your serial device is working, then you need to quit of that application before using the serial port with Wiring also if the port is in use with Processing. Only one application can use a serial port at a time. The "Tools » Serial ports" menu will only list the ports that are currently available.
begin() Opens the serial port for reading and writing.
read() Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first checking available() to see if data is available.
write() Writes a byte to the serial port.
print() Writes data (int, float, byte, char, char[], numbers in base (DEC, BIN, OCT or HEX) or Strings to the serial port.
println() It works as the print method but prints a new line character at the end of each call.
available() Returns the number of bytes available.
peek() Peek the next byte of data in serial port buffer. This doesn't retrieve the actual byte from the buffer. Returns -1 If no data is available.
flush() Flushes the serial port buffer. Further calls to read() and available() will return data received after the flush() command was issued.
end() Closes the serial port.
Usage Application
Updated on July 07, 2011 11:08:52pm PDT

Creative Commons License