Microcontrollers and Interfacing Week 04 Environmental sensing: light and temperature College of Information Science and Engineering Ritsumeikan University 1
series and parallel circuits series circuit: The current through every component in a series circuit is the same. The voltage across a series circuit is the sum of the voltages across the individual components. 5V GND parallel circuit: 5V i x X Y i x = i y = i = V/R = 5/(r x + r y ) v i y 0V i v = IR = i r y = 5 (i r x ) The voltage across every component in a parallel circuit is the same. The current flowing through a parallel circuit is the sum of the currents flowing in the individual components. i x 5V GND i 5V 5V X Y i y 0V 0V i i x = V/R = 5/r x i y = V/R = 5/r y i = i x + i y 2
analogue input: creating a variable voltage a potentiometer is a resistor with a variable tap two terminals connect to either end of the resistor another terminal connects to a wiper the wiper can move between the two other terminals wiper 5V to generate a variable voltage connect the potentiometer between 0 V and 5 V the wiper provides the output voltage variable voltage analogue input GND A0 3
analogue input: creating a variable voltage bottom view the middle pin of the potentiometer connects to A0 4
analogue input: reading the voltage level void loop() { //... int value = analogread(a0); // 0..1023 float percent = (float)value * (100f / 1023f); float voltage = (float)value * (5f / 1023f); //... } a useful function: void loop() { int in; //... int out = map(in, inlo, inhi, outlo, outhi); //... } maps the in value from the range [inlo...inhi] to the range [outlo...outhi] 5
environmental sensing: measuring light level a photocell is a resistor whose resistance depends on light level 50kΩ R = high 1kΩ R = low light-dependent resistor more light less resistance how do we turn a changing resistance into a changing voltage? hint: compare with potentiometer (variable resistor) 6
photocell plus resistor = light-dependent voltage divider a photocell can act as one half of a potential divider like a potentiometer, but only one side changes resistance 7
photocell resistance is inversely proportional to light level note the spread of possible values at the high-resistance end 8
light sensor values are unpredictable several unknowns, including: what is the minimum input voltage? what is the maximum input voltage? we can make our program auto-ranging (or self-calibrating ) remember minimum and maximum values re-map the input to a fixed range (e.g., 0 to 100%) using map() to do this makes it very easy map(value, frommin, frommax, tomin, tomax) maps value from the input range [frommin, frommax] to the output range [tomin, tomax] 9
light sensor software int low = 1023; int high = 0; void loop(void) { int value = analogread(a0); if (value <= low ) low = value - 1; if (value >= high) high = value + 1; value = map(value, low, high, 0, 100); Serial.println(value); delay(250); } why are the two global variables initialised with the values shown? 10
environmental sensing: measuring temperature in 1994, measuring temperature suddenly became very easy... LM35 Precision Centigrade Temperature Sensors General Description The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage over linear temperature sensors calibrated in Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. The LM35 does not require any external aged in hermetic TO-46 transistor p LM35C, LM35CA, and LM35D are al plastic TO-92 transistor package. The L able in an 8-lead surface mount small ou plastic TO-220 package. Features n Calibrated directly in Celsius 11(Centi
NS Package Number H03H LM35 converts temperature to voltage level TO-92 Plastic Package very low self-heating, less than 0.1 C in still air. The LM35 is rated to operate over a 55 to +150 C temperature range, while the LM35C is rated for a 40 to +110 C range ( 10 with improved accuracy). The LM35 series is available pack- Typical Applications S DS005516-2 rder Number LM35CZ, LM35CAZ or LM35DZ NS Package Number Z03A DS005516-3 FIGURE 1. Basic Centigrade Temperature Sensor (+2 C to +150 C) you can simply replace your potentiometer in last week s circuit with the LM35 be very careful to connect it correctly, or it will explode the +V S pin must go to the 5 V supply the V OUT pin connects to A0 (and GND connects to GND, obviously) your analogue input program from last week will work unmodified 12
temperature sensor software LM35 data sheet specifies its output as 0 V + 10 mv/ C what is the value of analogread(a0) at room temperature? what is the value of analogread(a0) at 10 C higher? we can reuse the analogue input software but output will be an integer between 0 and 1023, representing input voltage use the map() function to convert input voltage level to millivolts, and to convert millivolts to degrees, or to convert input voltage level to degrees 13