260 likes | 647 Views
MAE 4316 Mechatronics. Digital and Analog I/O on the Arduino platform. What is Digital ? . A Digital Variable can only acquire one of two distinct values , called “logic 1” and “logic 0” A digital SIGNAL is a stream of digital values.
E N D
MAE 4316 Mechatronics Digital and Analog I/O on the Arduino platform
What is Digital ? • A Digital Variable can only acquire one of two distinct values , called “logic 1” and “logic 0” • A digital SIGNAL is a stream of digital values. • A Digital circuit is such that its output(s) can only have two states high (1) or low (0) • In actual implementation, ones and zeros are represented by a High voltage and Low voltage • High and Low are relative to an arbitrary ground
Digital input / output with Arduino • The digital pins on the Arduino are have two modes • Input – where CPU measures the state of that pin • Output – where the CPU changes the state of that pin • The states are measured relative to the ground pin
Digital input / output with Arduino • Built in functions for digital i/o • The blink example made use of digital output • pinMode() , digitalWrite() , digitalRead() • The digital pins on Arduino have two modes: • Input – the CPU measures the state of that pin • Output – where the CPU changes the state of that pin • States are measured relative to the ground pin
Functions fordigital I/O pinMode(pin,direction) – sets the pin # pin to either input or output digitalWrite(pin,value) – sets pin whose directions is set to output by pinMode to either HIGH or LOW state = digitalRead(pin,value) – reads the state of a pin whose directions is set to input by pinMode() to input and returns either High or LOW
Example • On the Arduino IDE, open example named button • File->Examples->2.Digital->Button • This example illuminates the LED attached to pin13 depending on state of pin2 • The state of pin2 will be changed using a button • When the button is pressed the read point is 5v (or HIGH) and when released the read point is 0v (or LOW) • The state of this point will be read by pin 2 and the state of pin 13 changed accordingly This is connected to pin 2 on the Arduino
Lets look at the code // constants are used to define pin numbers: // this can also be achieved using #DEFINE const intbuttonPin = 2; // pin connected to the push button const intledPin = 13; // pin connected to the LED // variables are memory locations subject to change: intbuttonState = 0; // variable for reading push button status void setup() { // initialize the LED pin as output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as input: pinMode(buttonPin, INPUT); } void loop(){ // read the state of the push button pin buttonState = digitalRead(buttonPin); // check if push button is pressed. // if it is, the buttonState variable is set to HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } } • The three main sections of a sketch • Variables • void setup() • void loop() checks the state of pin2 and stores it in variable named ‘buttonState’ • As long as the button is pressed, the LED remains on • if-else syntax is like that of C • HIGH and LOW are pre-declared constants within the environment • Note: “==“ is a comparison “=“ is an assignment
10 k ohm resistor. Note the pin next to pin 2 is a ground This is the vcc pin which outputs 5 v LED off while button not pressed LED lights up while button is pressed
ANALOG I/O • Analog signals map to real numbers one to one Between two analog numbers there’s always another one ! • Physical magnitudes in the macroscopic world are analog in nature • Example: sound waves are a continuous variation of pressure that travel through air • When picked up by a sensor (microphone) they are converted to a continuously voltage signal that varies in time • Sensors typically output an analog voltage
Being Digital in an Analog world • Microcontrollers are digital in nature • Some way is needed to convert analog signals to a digital environment • This conversion is handled by a device called Analog to Digital converter
ADCs • Components of an AD conversion system • Voltage reference: (+), (-) • AD conversion chip • Conversion clock • Internal clock (CPU clock + divider) • External clock • Hardware internal RC clock • Output register • Output bus • Handshaking or command lines
Sensor example No. 1 • A typical sensors provides a voltage that changes based on what is being measured • Potentiometers – varies voltage based on position of a knob or slider • E.g. A volume control knob is often a pot
Reading Analog Signals with Arduino • The Arduino Pro Mini has a built-in 4-channel 10-bit ADC (pins A0 to A3) • The functions to access the ADCs are: Value = analogRead(pin) Value ranges from 0-1023 (10bit) with 0 = 0v and 1023 = 5v • Arduino AD conversion takes typically 100µs to complete depending on selected clock source
Example • Open the AnalogRead example • File -> Examples -> 3. Analog -> AnalogRead • This example is a variation of the “blink” example • The blink rate of the LED is adjusted by reading the analog pin using AnalogRead • The example calls for the use of a potentiometer but we’ll modify it • Instead we’ll use a light sensor the blink rate will change depending on the brightness of the room !
Analog Example Circuit • The light intensity sensor in your kit is called a photo resistor • The resistance of a photo resistor depends on the intensity of light falling on it • To measure this effect, a circuit is used called a voltage divider: The voltage at Vout depends on the values of the two resistors in the circuit • Since in this case one resistor is variable Vout will change proportional to the light intensity • From basic circuit theory: (R*Vin)/(R+Rphoto) = Vout
Implementation • Use R = 10 k ohm • Connect Vout to Arduino pin A0 • Compile and run the example AnalogIn • The green LED blink rate will change according to the ambient light intensity • You can slow the blink rate by covering the photo resistor
The code intsensorPin = A0; // select input pin for the // potentiometer intledPin = 13; // select input pin for the LED intsensorValue = 0; // variable to store the analog // voltage from the sensor void setup() { // declares ledPin as OUTPUT: pinMode(ledPin, OUTPUT); } void loop() { // read the voltage value from sensor sensorValue = analogRead(sensorPin); // turn the ledPin on digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> msecs delay(sensorValue); // turn the ledPin off: digitalWrite(ledPin, LOW); // stop the program for <sensorValue> msecs delay(sensorValue); } • This is a slight modification of the Blink example • sensorValue = analogRead(sensorPin); reads the voltage of sensorPin and stores it in sensorValue. It is between 0-1023 • SensorValue is then used in the delay fucntion delay(sensorValue); • Notes • Unlike digital IO there is no pinMode() needed for Analog IO • The pinMode() here is for the LED • The number returned by analogRead() is the raw ADC value. If we wanted to know the actual light intensity (in lumens for example) further calculations are needed