Upload
wingston-sharon
View
235
Download
0
Embed Size (px)
Citation preview
8/3/2019 Interfacing Peripherals on the arduino
1/37
8/3/2019 Interfacing Peripherals on the arduino
2/37
Temperature sensors
0 Gives a Vout propotional to ambient roomtemperature.
0 Concept : as temperature increases, voltageacross a p-n junction increases at a knownrate. (in this case Vbe)
0 Voltage at Anlg.Pin in Volts = (readingfrom ADC) * (5/1024)V
0 Centigrade temperature = [(analog voltagein V) 0.5] * 100
8/3/2019 Interfacing Peripherals on the arduino
3/37
Before: Writing the code
0 Review:
0 How does the lm35 work?
0
Read datasheet of lm35.0 How to connect it to arduino?
0 What reference to use?
0 What sort of noises will be present?
0
What does the analogue input signify?
8/3/2019 Interfacing Peripherals on the arduino
4/37
Datasheet of LM35
8/3/2019 Interfacing Peripherals on the arduino
5/37
Circuit connection diagram
8/3/2019 Interfacing Peripherals on the arduino
6/37
Temp sensor code
0 Connect lm35 as per circuit diagram in 7. Temp Sensor
folder.
0 Were giving AREF as 3.3V to reduce noise from the
LM35 and give more accurate readings.
0 Upload code onto Arduino and Output is seen in theSerial monitor of the Arduino.
8/3/2019 Interfacing Peripherals on the arduino
7/37
Write the code
1. Decide which analogue pin youre going to andassign it to a global variable
2. in setup() { }, initialize serial port to 9600 baud rate
to send temp reading out to PC.
3. In loop() { }, take the analogue reading from a pinand store it in a tempfloatvariable.
4. Multiply reading by 5.0 and then divide by 1024 to
get voltage value read by the port. Store this is afloat variable called volt.
5. Print out this value to the serial port and check.
8/3/2019 Interfacing Peripherals on the arduino
8/37
Keep Writing the code
0 To get temp in centigrade, (volt 0.5) * 100
0 This is the ambient temperature recognized by theLM35 in Celsius.
0 Send it to the serial port and check your code.
0 To convert to fahrenheight, mult by 9 then divide by 5then add 32 to that result. Send it to serial port too.
0 Congratulate yourself please.
8/3/2019 Interfacing Peripherals on the arduino
9/37
Improvements?
0 Use 3.3V as reference to increase resolution sincedatasheet says that resolution of lm35 ~ 10mV.
0Put in on PCB to avoid noise by breadboard internalresistance,
0 DO NOT LIGHT THE LM35 OR IMMERSE IT IN WATER.
0Make a waterproof casing for the lm35 to test temp ofliquids.
0 Any other applications?
8/3/2019 Interfacing Peripherals on the arduino
10/37
LDR
0 An LDR is a Light Dependent Resistor.
0 As in the resistance offered by this device to an
electrical circuit is dependent on the amount of lightfalling on the circuit.
0 Also called photocells because they convert light
energy to an electrical response.
0 Useful for on-when-light/off-when-dark.
8/3/2019 Interfacing Peripherals on the arduino
11/37
8/3/2019 Interfacing Peripherals on the arduino
12/37
How to connect the LDR
0 LDR is basically a resistor, so it can be connectedanyway.
0 You need a pull-down resistor to avoid sinking all thecurrent onto the arduino pin.
0 Circuit is GND-10kohn-LDR-VCC.
0 This circuit will only work if between 0-5V. If light istoo bright, it will saturate at 5V.
0 putting a variable resistor in place of 10k allows forcontrolling saturation with too bright/too dark light.
8/3/2019 Interfacing Peripherals on the arduino
13/37
Ambient
light like
Ambient
light (lux)
Photocell
resistance
()
LDR + R
()
Current
thru LDR
+R
Voltage
across R
Dim
hallway0.1 lux 600K 610 K 0.008 mA 0.1 V
Moonlit
night1 lux 70 K 80 K 0.07 mA 0.6 V
Room 10 lux 10 K 20 K 0.25 mA 2.5 V
Dark
overcast
day /Bright
room
100 lux 1.5 K 11.5 K 0.43 mA 4.3 V
Overcast
day1000 lux 300 10.03 K 0.5 mA 5V
8/3/2019 Interfacing Peripherals on the arduino
14/37
8/3/2019 Interfacing Peripherals on the arduino
15/37
Writing code!
0 decide analoge pin used as input to LDR.
0 In setup(){ }, initialize serial port with 9600.
0 In loop(){ }, first do an analogueread(pin) and store it in anintvariable called reading.
0 This value represents illumination received by LDR.
0 Send it to Serial Port.
0 Allow delay of some time to let the ADC settle.
8/3/2019 Interfacing Peripherals on the arduino
16/37
Application 1
0 Make thresholds and determine the brightness
received and send it via serial port.
if (reading < 10) { Serial.println(" - Dark"); }
else if (reading < 200) { Serial.println(" - Dim"); }
else if (reading < 500) { Serial.println(" - Light"); }else if (reading < 800) { Serial.println(" - Bright"); }
else { Serial.println(" - Very bright"); }
8/3/2019 Interfacing Peripherals on the arduino
17/37
Application 2
0 Take the analogue reading and use it as the delay
amount in an LED blinking scenario. Use ledpin = 13.
void loop()
{
val = analogRead(LDR);
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);delay(val);
}
8/3/2019 Interfacing Peripherals on the arduino
18/37
Another Application
0 Put three LEDs around the LDR. Red, Green and Blue.
0 Put an object of a particular color in front of the LDR.
0 Light up RED LED and measure LDR value
0 Light up GREEN LED and measure LDR value
0 Light up BLUE LED and measure LDR value
0 From these three analogue values you can determine thecolor of object being sensed.
8/3/2019 Interfacing Peripherals on the arduino
19/37
Sensor Data on PC
0 Reading and plotting sensor data on PC is very simple
on the arduino.
0 Send data continuously to the computer over the
serial port.
0 Data can be viewed by any program capable ofviewing this data
8/3/2019 Interfacing Peripherals on the arduino
20/37
Send sensor data to serial
port0 Open the folder Sensor data graph
0 Upload the arduino code onto the arduino and close
arduino IDE after checking serial monitor.
0 In softwares folder extract the processing folder.
0 Open processing and copy the processing code onto it and
run.
0 Make sure serial port number in the processing sketchmatches arduino
8/3/2019 Interfacing Peripherals on the arduino
21/37
The motor shield
1. DC Motor InterfaceDrives 2 motors with L293D DC MotorDriver IC.
2. IR paired sensorsFor line sensing, 4 IR sensor pairs can beattached and controlled.
3. The Buzzer or Servo Motor
Selectable via jumper
4. The Switch or Servo Motor
Selectable via jumper
5. Two LEDs
The motor shield goes on top
of the arduino neatly.
There are loads of shields for
the arduino that provide
different functionality.
Be careful removing the
shield, if you bend the pins,
you cant put the shield in
again.
8/3/2019 Interfacing Peripherals on the arduino
22/37
8/3/2019 Interfacing Peripherals on the arduino
23/37
8/3/2019 Interfacing Peripherals on the arduino
24/37
IR TX RX Pair
0 An IR transmitter (TX) is a LED that emitslight in the infrared spectrum.
0 (most cameras see IR, light up the IR LED
and point a camera at it to check)
0 An IR Receiver (RX) is a transistor with thebase exposed.
0 The base voltage depends on the amount of
IR received and it controls the amount ofcurrent passing through the C-E junction(leads).
8/3/2019 Interfacing Peripherals on the arduino
25/37
IR schematic
0 The IR TX LED is connected through a current
limiting resisto
0 The IR RX is connected via a variable potentiometer
to a voltage comparator to give a digital output.
0 The pot is used to adjust
For ambient light intensity.
8/3/2019 Interfacing Peripherals on the arduino
26/37
Remember
0 The IR TX RX pairs should be spaced 1/4th of an inch apartfor optimal detection.
0 Calibration of IR sensors is important to adjust the sensorsfor ambient light.
0 The output of this circuit is DIGITAL and read by a digitalpin. 1 obstruction; 0 No obstruction.
0 If you have to use IR sensors in the sun, use electrical tapeto provide adequate shielding.
8/3/2019 Interfacing Peripherals on the arduino
27/37
Using the IR sensors
0 The motor shield has 4 IR sensors and 4 pots.
0 Connect the IR dongles to the placeholders. They go in onlyone way
0 There are status LEDs just next to each IR socket, if thecomparator goes HIGH, the LED lights. This is forcalibration.
0 Keep the IR sensor pointed at nothing (no obstruction).
0 Use a screwdriver and GENTLY turn the appropriate POT
until the respective IR sensors status LED turns dark.0 Check with your hand if LED lights, if not readjust as you
like.
8/3/2019 Interfacing Peripherals on the arduino
28/37
PINS
0 The table below shows the pin arrangement for the 4sensors on the shield.
0 If the shield is mounted, then these 4 pins provide theoutput of the comparator.
0 Use digitalread(pin) to see the state of the pin at any
time.
8/3/2019 Interfacing Peripherals on the arduino
29/37
Try
0 Connect all four IR sensors and calibrate them.
0 Write a program to output which of the 4 IR sensorshas gone HIGH to the serial port.
0 CHALLENGE: Try and make sure that the message onthe serial port doesnt repeat until the IR sensor has
gone LOW again (obstruction removed). It shouldn'tcontinuously output the message if the IR sensor isobstructed,
8/3/2019 Interfacing Peripherals on the arduino
30/37
DC motors
0 Motors are devices that convert electrical energy tomechanical energy.
0
Motors come in different varieties: AC, DC, Servo, Stepperetc
0 If motor draws less than 40mA of current, it can be directluwired to an arduino pin.
0 Current drawn by a DC motor is propotional to speed andtorque. To avoid burning out the arduino, we use motordrivers to control DC motors.
8/3/2019 Interfacing Peripherals on the arduino
31/37
The L293 driver
0 This chip isolates logic level voltages and the motor supply
voltages.
0 It as also called a H-bridge.
0 It uses the transistors as a switch concept to electrically
switch a DC power supply onto the terminals of the motor.
0 The driver can control two motors independently from the
same supply.
8/3/2019 Interfacing Peripherals on the arduino
32/37
Motor Driver
8/3/2019 Interfacing Peripherals on the arduino
33/37
The Motor Shield
0 The motor driver is present on theshield which also contains a wireblock for easy connection of thewires.
0 The supply will have terminalsmarked on the board. Do not reversethese connections.
0 Motor can be connected in anyway.The direction might have to becalibrated by testing if the terminalsare reversed.
+
-
8/3/2019 Interfacing Peripherals on the arduino
34/37
Arduino shield to Motor
Driver Pinout0 To use a motor, set the enable pin to high in the setup() { }
0 Motor movement is done in loop() { }
0 If l1 is HIGH and l2 is LOW, motor turn in one direction.
0 If l1 is LOW and l2 is HIGH, the motor turns in the otherdirection.
0 If both l1 and l2 are set to HIGH or LOW, then motor is stopped.
8/3/2019 Interfacing Peripherals on the arduino
35/37
Wiring motors
0 Attach the arduino shield onto the arduino.
0
Take a 9V battery cap and screw it onto the motorsupply terminals. Take care of polarity.
0 Red >> + and Black >> -
0Wire up the motors and connect them to theterminals.
8/3/2019 Interfacing Peripherals on the arduino
36/37
TRY
0 Move both motors in same direction.
0
Reverse both motors.
0 Stop one and turn the other.
0 Stop the other and turn the one.
0 Make each motor turn in a different direction.
8/3/2019 Interfacing Peripherals on the arduino
37/37
Exercise
0 Combine the functionality of 2 IR sensors and the
motors to make an obstacle avoider bot.
0 Algorith:
0 Move forward, left and right randomly.
0 If IR sensors detect an obstruction, move a little back in
the opposite side of the obstruction
0 Continue.