Introduction to Sensor Technology Week Four Adam Taylor

Slides:



Advertisements
Similar presentations
Wireless Cue Light Project
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Lab7: Introduction to Arduino
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
How to use Arduino By: Andrew Hoffmaster.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Parallax 4x20 LCD (part number 27979) with Arduino Duemilanove
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and.
Arduino Part 2 Topics: Serial Communication Programming Constructs: functions, loops and conditionals Digital Input.
Colorado Space Grant Consortium Gateway To Space ASEN 1400 / ASTR 2500 Class #12 Gateway To Space ASEN 1400 / ASTR 2500 Class #12 T-58.
Basic Circuits – Lab 2 Arduino and Sensors
DPNM Lab., POSTECH 1/25 CS490K - Internet of Things (IoT) Jonghwan Hyun DPNM Lab. Department of Computer Science and Engineering, POSTECH
Tele-presence – Connecting Two Processing Platforms via Internet Controlling the Screen based on Distant User Input ServerClient 1.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Intro to the Arduino Topics: The Arduino Digital IO
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Data Representation and Storage Lecture 5. Representations A number value can be represented in many ways: 5 Five V IIIII Cinq Hold up my hand.
ATLAS 2013 Super Fast Intro to Arduino and Processing Materials by Lindsay Craig, David Stillman and Ben Leduc-Mills.
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 1 Getting Started to Arduino.
Computer Science Binary. Binary Code Remember the power supply that is inside your computer and how it sends electricity to all of the components? That.
Code The Arduino Environment.
Robootika lahenduste esitlus Raul Liinev Martin Ligema Siim Suu Martin Tõnne.
Serial Communication. mouseX from computer to arduino processing sends a single byte of data arduino reads each byte arduino uses value to set light brightness.
INFORMATION TECHNOLOGY
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
1 - Remove LED from 13 and GND - Bring out your breadboard from HW#4 Arduino Overview:
Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011.
Welcome to Processing Who does not have access to digital camera?
C# SERIAL COMMUNICATION TEMPERATURE CONTROL WITH ARDUINO KAAN EREN
Serial Communication RS-232. In order to make two devices communicate, whether they are desktop computers, microcontrollers, or any other form of integrated.
Microcontroller basics Embedded systems for mortals.
Microcontroller basics Embedded systems for mortals.
09/06/ Data Representation ASCII, Binary Denary Conversion, Integer & Boolean data types.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Arduino + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Understanding Computers
Arduino Programming. THE ARDUINO IS A MICROCONTROLLER – A LOW COST, LOW PERFORMANCE COMPUTER.
Hacking on Arduino George Patterson
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Using a SparkFun™ serial LCD with an Arduino
Lab 7 Basic 1: Game of Memory
Understanding binary Understanding Computers.
Assist. Prof. Rassim Suliyev - SDU 2017
How Computers Store Variables
UNDERSTANDING YOUR COMPUTER
Wireless Cue Light Project
Arduino Programming Part II
Microcontroller basics
Numerical Representation
Tele-presence – Connecting Two Processing Platforms via Internet
UCD ElecSoc Robotics Club 2017/2018
Arduino Part 1 Topics: Microcontrollers Programming Basics
3.1 Serial Communication Part 1
Roller Coaster Design Project
Roller Coaster Design Project
Schedule 8:00-11:00 Workshop: Arduino Fundamentals
Intro to the Arduino Topics: The Arduino Digital IO
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Debugging Debug environments Debug via serial
Programming 2: The Arduino IDE & First Sketches
Numerical Representation
Appliace Remote Control
Presentation transcript:

Introduction to Sensor Technology Week Four Adam Taylor

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.

Serial Communication

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

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

Serial vs. Parallel Communication

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.

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.

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

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

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)

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

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

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

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

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

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.

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

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

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

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 }

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

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.

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

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

Data Types

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++; }

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.

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

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.

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

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

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.

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’

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.

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

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.

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.

Arduino Code

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.

Example Three: Arduino Code

Example Three: Processing Code