Galileo Tutorial I/Os, Sensing and Actuation Senzations 2014 Biograd na Moru 1. September 2013 Alex Gluhak Intel Labs Europe 1 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Learning goals I/O Basics Available I/O options on the Galileo Serial I/O Sensing and actuating Grove starter kit Digital I/O Analog I/O 2 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Available I/O options on the Galileo 3 Galileo Tutorial I/Os, Sensing and WHAT Actuation WILL YOU MAKE? WHAT WILL YOU MAKE? 3
I/O options on the Galileo RS232 Serial USB client USB host 14 Digital I/Os I2C/TWI SPI Arduino Uno 3 Compatible Headers JTAG 6 Analog Inputs 4 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial communications on the Galileo 5 Galileo Tutorial I/Os, Sensing and WHAT Actuation WILL YOU MAKE? WHAT WILL YOU MAKE? 5
Serial communications (1/2) Arduino platforms have at least one serial port (also known as a UART or Universal Asynchronous Receiver/Transmitter) Useful for communications between the Arduino board and a computer or other devices Galileo provides several serial communication options Via RS232 serial port on 3.5mm phone jack connector (Linux console) Via USB client port (Sketches) Via UART on digital pins 0 (RX) & 1 (TX) Linux Console Serial Sketches Serial1 6 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial communications (2/2) Initialising serial read and/or write interactions Arduino Syntax: Serial.begin(baudrate) Serial.begin (9600) // opens a serial port with 9600 bauds per sec Baud rates supported on Galileo: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. 7 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial write (1/2) Print data as human readable ASCII to serial port Arduino Syntax: Serial.print(val) or Serial.print(val, format) Arduino Syntax: Serial.println(val) or Serial.println(val, format) Serial.print ( Senzations 2014 ) // prints Senzations 2014 Serial.println (3.14159265359, 2) // prints 3.14 with CR/LF Parameters Val data to be printed (any type) Format number base or number of decimal places to be printed 8 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial write (2/2) Write binary data to serial port Arduino Syntax: Serial.write(val) Serial.write ( Senzations 2014 ) // writes Senzations 2014 Data is sent as byte or series of bytes 9 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial read (1/2) Data sent to serial arrives into the serial receive buffer Size: 64 bytes Good practice is to check first the availability of data Syntax: Serial.available() Return number of bytes Data can be read either byte by byte Arduino Syntax: Serial.read() Returns a byte that has been read 10 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Serial read (2/2) Data can also be read in chunks into a buffer Arduino Syntax: Serial.readBytes(buffer, length) Parameters Buffer: to store the read data, a char[] or byte[] Length: number of char to read Returns the number of char/bytes placed in the buffer Terminates when length is reached or time out occurs, you can set the timeout as follows: Arduino Syntax: Serial.setTimeout(time) // in milisec 11 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
User built-in serial monitor Select Tools -> Serial Monitor or click Serial Monitor Icon Note: Sketch must be loaded first; else, Serial Monitor will close on Sketch upload 12 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Exercises Store your name(s) in a variable and write it character by character with a delay using all different serial write options Write all prime numbers between 1 and 50 via the serial interface in a single coma separated line Write a simple echo server that returns all data send via serial port (char by char and all at once) Try to use an alternative terminal for receiving data, not the built in one 13 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Grove starter kit Sensors and actuators used in the tutorial 14 Galileo Tutorial I/Os, Sensing and WHAT Actuation WILL YOU MAKE? WHAT WILL YOU MAKE? 14
Grove starter kit 15 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Sensors / input devices Sound sensor Analog Rotary angle sensor Button Digital Temperature sensor Light sensor Touch sensor 16 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Actuators / output devices Relay switch LCD display (I2C) Buzzer LED Servo 17 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Base shield 7 Digital I/Os (D2.D8) 1 UART (1 port) 4 Analog inputs (A0.A3) ISP (1 port) Power 3.3/5V switch Reset button 1 I2C (4 ports) 18 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Principles HW interfacing Select appropriate port to plug in the sensor 4 wire interconnects (GND, VCC, data1, data 2) SW support Most sensors work out of the box with standard Arduino I/O Additional libraries needed for Servo LCD display 19 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Digital I/O on the Galileo 20 Galileo Tutorial I/Os, Sensing and WHAT Actuation WILL YOU MAKE? WHAT WILL YOU MAKE? 20
Digital I/Os 14 Digital I/O pins which can be configured as either input or output 12 can serve as GPIOs (Pins 2-13) 2 can support UART (Pin 0 and 1) 6 can serve as PWM (Pins 3,5,6,9,10,11) - ~ Operating parameters Can work at both 3.3V and 5V (jumper to configure) Max (out/in): 10mA / 25mA Max total on all digital (out/in): 40mA/100mA 21 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Configuring GPIOs GPIOs can be configured as either input or output Arduino Syntax: pinmode (pin, mode) pinmode(13, OUTPUT) // set pin 13 to output mode pinmode(4, INPUT) // set pin 4 to input mode 22 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Digital write Allows pins configured as output to be set either HIGH or LOW Arduino Syntax: digitalwrite(pin, value) digitalwrite (9, HIGH) // 3.3/5V available on Pin9 digitalwrite (9, LOW) // 0V available on Pin9 23 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Exercise 1 Digital write Write a sketch that toggles the LED every 2 seconds Hints: LED is connected to Pin 13 Use: Delay ( [ms] ) for waits 24 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Digital read Allows pins configured as input to be read Arduino Syntax: digitalread(pin) Returns value either LOW or HIGH Example digitalread (8) // returns either low or high state available on Pin8 25 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Exercise 2 Digital read/write Variation 1: Turn on a led if a button has been pressed + Variation 2: Turn on buzzer if a the touch sensor is pressed + Hint: Use any of the digital PINs D2-D8 26 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Solution 27 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Analog I/O on the Galileo 28 Galileo Tutorial I/Os, Sensing and WHAT Actuation WILL YOU MAKE? WHAT WILL YOU MAKE? 28
Analog I/O 6 Analogue inputs (Pins A0-A5) AD7298 analog-to-digital (A/D) converter, 12bit Measures from 0 to 5V by default, controlled by IOREF Resolution is limited to 10bits by default 1024 units -> at 5V = 4.9mV per unit Readings mapped into integer values from 0 1023 Analog pins can be configured to act as digital pins A0 -> PIN14. A5->PIN19 Analog output via PWM 29 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Analog read Reading voltage level attached to a analogue pin Arduino Syntax: analogread(pin) Returns integer value between 0 1023 analogread (A1) // reads analog value of pin AP1 Mapping values to other voltage ranges Arduino Syntax: map(value, fromlow, fromhigh, tolow, tohigh) map(545, 0,1023, 0, 255) // reads analog value of pin AP1 30 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Exercise Analog read Connect analog sensor to an analog input and read out Available options: Light sensor Noise sensor Temp sensor Rotation sensor Hint: Use any of the analog PINs A0-A3 31 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Solution 32 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Pulse Width Modulation (PWM) Technique for getting analog results with digital means PWM is supported on Pin 3, 5, 6, 9, 10, and 11 marked with a ~ Usage examples Light a LED at varying brightness Drive a motor at various speeds 33 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
PWM principles Digital pins are switched between HIGH and LOW to generate a square wave This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends between the two states To get varying analog values, you change, or modulate, that pulse width Source: http://arduino.cc/en/tutorial/pwm 34 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Analog write Invoke analog write on a PWM pin to generate an analog value, which depends on the pulse width Arduino Syntax: analogwrite(pin, value) analogwrite (10, 32) // send PWM with 12.5% duty cycle to PIN10 Range of value parameter is 0 255: analogwrite of 255 requests a 100% duty cycle (always on) analogwrite of 127 is a 50% duty cycle (on half the time) analogwrite of 0 is a 0% duty cycle (Always off) 35 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Exercise Analog write Modulate the intensity of the LED light Use the following options Light sensor Rotation sensor Hint: Use map to adjust output to input values 36 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Solution Analog write 37 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?
Resources and further reading Grove Starter Kit Wiki: http://www.seeedstudio.com/wiki/grove_-_starter_kit_v3 Grove starter kit sketch book https://github.com/seeed-studio/sketchbook_starter_kit_v3.0 38 Galileo Tutorial I/Os, Sensing and Actuation WHAT WILL YOU MAKE?