introduction to sensor technology week four adam taylor [email protected]

53
Introduction to Sensor Technology Week Four Adam Taylor [email protected]

Upload: carmella-mcdaniel

Post on 19-Dec-2015

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Introduction to Sensor Technology

Week Four

Adam Taylor [email protected]

Page 2: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Week 4.1 Serial Communication

• The word serial means ‘one after the other’. Serial data transfer is when we transfer data one bit at a time, one right after the other. Information is passed back and forth between the computer and Arduino by setting a pin high or low.

Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it.

Page 3: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Serial Communication

Page 4: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Data measurements:

A single bit is either a zero or a one. You can group bits together into 8 bits which is 1 byte. 1024 bytes (8192 bits) is one Kilobyte (sometimes written KiB or KB).1024 KiB (1048576 bytes) is one Megabyte (MiB or MB)1024 MiB is 1 Gigabyte (GiB or GB)

Page 5: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Data measurements:

Sometimes 1000 is used as a base instead of 1024 (particularly by people who sell hard drives).1000 bytes (8000 bits) is one Kilobyte (sometimes written KB).1000 KB (1,000,000 bytes) is one Megabyte (MB)1024 MB is 1 Gigabyte (GB)

Page 6: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Serial vs. Parallel Communication

Page 7: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Serial Communication

• We’ve already used the serial communication capability – this is how we upload sketches to Arduino. This connection can also be used by the sketches we write in arduino to send data back to the computer or receive commands from it. For this purpose we are going to use a serial() function.

Page 8: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

• If we can use Arduino to write value to the serial port, we can monitor the results of our work and debug our applications more effectively.

• Other applications that can also read from the serial port can access the data being read by Arduino i.e. switch state, potentiometer values etc.

• Similarly, if we can read from the serial port, we can receive feedback and instructions from other applications.

Page 9: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Week 4.2 Serial Functions

• Serial.begin(speed)• Serial.begin()

• prepares Arduino to begin sending and receiving data. It takes one argument which specifies the no. of bits per second (bps) we receive. We refer to the bps as the baud rate.

• We generally use 9600 bps with Arduino.• We specify this in Void setup()

Page 10: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example:

• Serial.begin (9600);

/* Open the serial port to send data back to the computer at a rate of 9600 bps. */

Page 11: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

• Serial.print(data)• Serial.print(data, encoding)

This function sends data to the serial port.

The encoding argument is optional; if not supplied, the data is treated as much like plain text as possible.

Examples: BIN (binary), DEC (decimal)

Page 12: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example:

• Serial.print(75); // Prints “75”

• Serial.print (75, BIN); Prints “1001011”(75 in Binary)

Page 13: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

• Serial.println(data)• Serial.println(data, encoding)

• This is the same as Serial.print() except that it adds a carriage return and line feed (\r\n) as if you had typed in the data and then pressed Return or Enter.

• Serial.println is short for ‘print line’.

Page 14: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example:

• Serial.println(75); //prints “75\r\n”

This works as though you had typed 75 andthen pressed enter for a new line.

Serial.print(“potentiometer value is “);Serial.println(variablename, DEC);

Page 15: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

• Serial.write ()

As of the new arduino 1.0 version, Serial.write() is used to send data as bytes to the serial port.

Page 16: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example:

• Serial.write(val) // a value to send as a single byte

• Serial.write(str) // a string to send as a series of bytes

• Serial.write(buf, len) /* buf: an array to send as a series of bytes len: the length of the buffer */

Page 17: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Int Serial.available()

• Returns how many unread bytes are available on the Serial port for reading via the read() function. This is useful if you are communicating with another application i.e. Processing.

• After you have read everything available, Serial.available() returns 0 until new data arrives on the serial port.

Page 18: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Int Serial.read()

• Reads from the serial port:• Fetches one byte of incoming data.• For listening to values from the serial port.

Page 19: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example:

• if(Serial.available() != null) {Serial.read(); }

/* If the data received from Serialavailable() is not 0, read the available data.*/

Page 20: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Serial Monitor

After you’ve uploaded the code to your Arduino, press the “Serial Monitor” button on the Arduino IDE (the rightmost button in the toolbar).

Page 21: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

void setup(){Serial.begin(9600); // send and receive at 9600 baud} int number = 0; void loop(){Serial.print("The number is "); Serial.println(number); // print the numberdelay(500); // delay half second between numbers number++; // to the next number}

Page 22: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

/* Send to the computer the values read from analog input 0.Be sure to click the serial monitor button (right most)after you upload */

#define SENSOR 0 // select the input pin for the sensor resistorint val = 0; //variable to store the value coming from // the sensor void setup() { Serial.begin(9600); //open the serial port // to send data back to the computer // at 9600 bits per second }void loop() { val = analogRead(SENSOR); // read the value from the SENSOR Serial.println(val); //print the value of the serial port delay(100); //waits 100 ms between each send }

Page 23: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

W.4.3. Constructing and Parsing Messages

We want to make sure that the information we send over the serial port can be easily extracted and understand by the software or hardware receiving it. This is straightforward when sending one single value, but becomes more complicated when we send strings and multiple values to different forms of hardware and software. It’s useful to understand different data types and to understand techniques for organising our messages.

Page 24: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Delimit values in Serial

• If you are reading more than one pin’s value to the serial port it is a good idea to delimit the values with a space:

• This is implemented as follows: Serial.print(“ “) produces a space.

Example:piezoval = analogRead(piezopin);LDRval = analogRead(LDRpin);Serial.print(piezoval);Serial.print(" "); //delimit the values with a spaceSerial.println (LDRval);delay(100);

Page 25: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Prepend Values

• You could also prepend the sensor value with a tag like A B and C as follows:

• Example: piezoval = analogRead(piezopin); //read value from piezoLDRval = analogRead(LDRpin); //read value from LDRSerial.print(“A”)Serial.print(piezoval);Serial.print(“B"); // prepend with ‘B’Serial.printIn (LDRval); delay(30);

Page 26: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Data Types

Page 27: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

char chrValue = 65; // these are the starting values to printint intValue = 65; float floatValue = 65.0;void setup(){ Serial.begin(9600);}void loop(){Serial.println("chrValue: "); Serial.println(chrValue); Serial.write (chrValue);Serial.println(); Serial.println(chrValue,DEC);Serial.println("intValue: "); Serial.println(intValue); Serial.write( intValue);Serial.println(); Serial.println(intValue,DEC); Serial.println(intValue,HEX); Serial.println(intValue,OCT); Serial.println(intValue,BIN);Serial.println("floatValue: "); Serial.println(floatValue);delay(1000); // delay a second between numbers chrValue++; // to the next valueintValue++;}

Page 28: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

ASCII Chart

• Serial information isn’t sent and received as characters and integers. Its converted into ASCII (the American Standard Code for Information Interchange).ASCII encoding dates to the 1960's. It is the standard way that text is encoded numerically.

• Note that the first 32 characters (0-31) are non-printing characters, often called control characters. The more useful characters have been labeled.

Page 29: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 30: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

W4.5. Arduino and other software environments

• Now any software that can read from the serial port can talk to Arduino (processing, Max/MSP, Pure Data...)

Example: Potentiometer controlling pitch Similarly, we can send any values from another

application to Arduino. Example: Mouse Movements controlling

brightness of an LED

Page 31: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Using the Serial port with other applications

Only one application at a time can use the Serial port. We upload the code to the Arduino, close the Arduino IDE down, open up Processing and run the code in Processing that reads our values from the serial port.

Page 32: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example One: Use Pushbutton to Control Visuals in Processing

Arduino Code:

// This is a simple bit of code that just fires off the digital // input from pin 2, as a serial port output to Processing. void setup() { Serial.begin(9600); //begin serial comm. At 9600 bps } void loop() { Serial.println(digitalRead(2)); //print value of pin 2 with line return // you can add the delay below //delay(1); }

Page 33: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example One: Accompanying Processing Code: (the serial commands are highlighted in red)

Page 34: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 35: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 36: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 37: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

The Code Explained

• This example used digital input to control a visual representation in processing.

• Arduino Code: The Arduino code reads in a value

from digital pin 2 and prints that value to the serial port.

Page 38: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Processing Code:

In processing we import the library used to read data from the serial port:

import processing.serial.*; // import processing's serial library used to read serial

We define the port we are going to use: Serial port; //declare the port we will use With this line we declared the serial port and called

it ‘port’

Page 39: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Just as in Arduino, we identify the port we want to receive from and transmit to. The code example suggests two ways of doing this.

println("Available serial ports:"); println(Serial.list()); port = new Serial(this, Serial.list()[0], 9600);

This prints the available serial ports as a list in processing and selects the first element from the list (element [0]). You may need to change this to correspond with whatever element of the list is your correct communication port. The first port in the list is element [0], the second is element [1] and so on.

Page 40: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Alternatively, if you know the name of your port, you can specify it directly. My port is usually Com 3 so I would specify it like this:

port = new Serial(this, "COM3", 9600);

Page 41: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

While data is available from the serial port we read this data.

while (port.available() > 0) serialEvent(port.read());

This calls the function serialEvent(), specified in the Processing code, which reads the message and parses it into content we want to use, stripping off the carriage return from the incoming string.

Page 42: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example Two: Using a Potentiometer to control Visuals

This example reads in the values from an analog sensor connected to analog pin 0 of the Arduino and sends them to processing where they are used to control a visual graph on screen.

Page 43: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Arduino Code

Page 44: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 45: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 46: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 47: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 48: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 49: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Sending Data to Arduino

• This example shows how to send data from a personal computer to an Arduino board to control the brightness of an LED. The data is sent in individual bytes, each of which ranges in value from 0 to 255. Arduino reads these bytes and uses them to set the brightness of the LED.

• You can send bytes to the Arduino from any software that can access the computer serial port.

Page 50: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example Three: Arduino Code

Page 51: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie
Page 52: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie

Example Three: Processing Code

Page 53: Introduction to Sensor Technology Week Four Adam Taylor tayloral@tcd.ie