Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Sensor Technology Week Four Adam Taylor

Similar presentations


Presentation on theme: "Introduction to Sensor Technology Week Four Adam Taylor"— Presentation transcript:

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

2 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.

3 Serial Communication

4 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)

5 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)

6 Serial vs. Parallel Communication

7 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.

8 If we can use Arduino to write value to the s erial port, we can monitor the results of our work and debug our applications more effecti vely. Other applications that can also read from the serial port can access the data being read by Arduino i.e. switch state, potentiomet er values etc. Similarly, if we can read from the serial p ort, we can receive feedback and instructio ns from other applications.

9 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()

10 Example: Serial.begin (9600); /* Open the serial port to send data back to the computer at a rate of 9600 bps. */

11 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)

12 Example: Serial.print(75); // Prints “75” Serial.print (75, BIN); Prints “1001011”(75 in Binary)

13 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’.

14 Example: Serial.println(75); //prints “75\r\n” This works as though you had typed 75 and then pressed enter for a new line. Serial.print(“potentiometer value is “); Serial.println(variablename, DEC);

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

16 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 */

17 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.

18 Int Serial.read() Reads from the serial port: Fetches one byte of incoming data. For listening to values from the serial port.

19 Example: if(Serial.available() != null) {Serial.read(); } /* If the data received from Serialavailable() is not 0, read the available data.*/

20 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).

21 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 number delay(500); // delay half second between numbers number++; // to the next number }

22 /* 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 resistor int 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 }

23 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 re ceiving it. This is straightforward when sending one single value, but becomes more complicated when we send strings and multiple values to different for ms of hardware and software. It’s useful to unde rstand different data types and to understand te chniques for organising our messages.

24 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 space Serial.println (LDRval); delay(100);

25 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 piezo LDRval = analogRead(LDRpin); //read value from LDR Serial.print(“A”) Serial.print(piezoval); Serial.print(“B"); // prepend with ‘B’ Serial.printIn (LDRval); delay(30);

26 Data Types

27 char chrValue = 65; // these are the starting values to print int 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 value intValue++; }

28 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.

29

30 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

31 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.

32 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); }

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

34

35

36

37 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.

38 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’

39 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.

40 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);

41 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.

42 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.

43 Arduino Code

44

45

46

47

48

49 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.

50 Example Three: Arduino Code

51

52 Example Three: Processing Code

53


Download ppt "Introduction to Sensor Technology Week Four Adam Taylor"

Similar presentations


Ads by Google