UCF - Arduino - Lesson 2

20
Working with Arduino: Lesson #2: Variable, Photo, and Force Sensitive Resistors EGN1007

description

UCF - Arduino - Lesson 2

Transcript of UCF - Arduino - Lesson 2

Page 1: UCF - Arduino - Lesson 2

Working with Arduino:Lesson #2: Variable, Photo, and Force Sensitive Resistors

EGN1007

Page 2: UCF - Arduino - Lesson 2

Learning Goals

Learning Goals: The student will be able to:1.Build a complete circuit using the Arduino microprocessor2.Identify important electrical components in a circuit and explain their use3.Identify and apply specific elements in “C” code used to run a program in conjunction with how a circuit is built

Page 3: UCF - Arduino - Lesson 2

Variable resistorsAlong with the digital pins on the Arduino, the board also

has 6 analog pins which can be used. These inputs take a voltage between 0 and 5 volts and convert it to a digital number 0 to 1024. A device which uses these inputs is called a POTENTIOMETER or variable resistor.

When a voltage is applied to the outer pins, the middle pin will read some value between 0-5 volts depending on the angle of the position.

The schematic symbol is that of a regular resistor with an arrow either through it or perpendicular to it.

Page 4: UCF - Arduino - Lesson 2

Let’s Begin1.Place the potentiometer on D5-D7.2.Run a red wire from the 5V on the Arduino to E5. This will supply the Pot with 5V.3.Run a wire from Analog 0 to E6.4.Place the LED on J5 and J6 with the longer lead(+) of the LED in J5. 5.Place a resistor on I6and I16. Notice that both the resistor and LED share row 56.Run a red wire from Digital 13 port on Arduino to I5.7.Run a black wire from H16 to the ground(GND).8.Run a black wire from E7 to G16.

Page 5: UCF - Arduino - Lesson 2

Programming our sketch

Let’s define 3 variables as integers.

These will tell us which ports our LED and POT are in

This is a variable that we will use which can store a value coming from the POT.

Page 6: UCF - Arduino - Lesson 2

Programming our sketch

Just like before we need to set our LED as an output.

Page 7: UCF - Arduino - Lesson 2

Programming our sketchThe sensorValue is determined by the actual READING on the POT

The only difference this time is that the delay is caused by the READING on the POT which is referenced by the sensor’s value.

Page 8: UCF - Arduino - Lesson 2

Your turn!

Using what you have learned in this lesson AND the last lesson. Write a short program that uses the POT to control the brightness of the LED. HINT: Do not allow it to blink!

Write your code on the lesson worksheet and explain your code.

Page 9: UCF - Arduino - Lesson 2

PhotoResistorsThis type of resistor produces a variable

resistance based on the amount of incident light.

The schematic symbol shows light in the form of arrows coming down and striking the resistor symbol.

To be used correctly is MUST be in a VOLTAGE DIVIDER.

Page 10: UCF - Arduino - Lesson 2

Voltage DividerA voltage divider is a simple circuit consisting of two resistors that has the useful property of

changing a higher voltage (Vin) into a lower one (Vout). It does this by dividing the input voltage by a ratio determined by the values of two resistors (R1 and R2):

So lets say that R1 is the photocell and R2 was a regular resistor. Ss the resistance of the photocell changes as a function of light intensity, the current flowing would increase and decrease. This will cause a voltage that CHANGES and changes in voltage is what Arduino can sense and understand.

Page 11: UCF - Arduino - Lesson 2

Let’s Begin1.Place the photoresistor on J20 & 21.2.Run a red wire from the 5V on the Arduino to J20. This will supply the PR with 5V.3.Place the 10K resistor (brown-black-orange) on I21 and I30.4.Run a wire from Analog 0 to H215.Place the LED on J5 and J6 with the longer lead(+) of the LED in J5. 6.Place a 560 resistor on I6 and I16. Notice that both the .resistor and LED share row 57.Run a red wire from Digital port 9 on Arduino to I5.8.Run a black wire from J16 to the ground(GND).9.Run a black wire from H30 to H16.

Page 12: UCF - Arduino - Lesson 2

Programming the sketchThese will tell us which ports our LED and PR are in.

Page 13: UCF - Arduino - Lesson 2

Programming our sketch

Just like before we need to set our LED as an output.

Page 14: UCF - Arduino - Lesson 2

Programming the sketch - mapmap(value, fromLow, fromHigh, toLow, toHigh)

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

Why do we have to do this? The LED understands the range 0 to 255 and the photoresistor will export values between 0 and 900

Page 15: UCF - Arduino - Lesson 2

Programming the sketch - constrainconstrain(x, a, b)

Constrains a number to be within a range.

Example

sensVal = constrain(sensVal, 10, 150);

// limits range of sensor values to between 10 and 150

Page 16: UCF - Arduino - Lesson 2

Why are we doing this?(I told I was going to explain this later)

Remember that the Arduino is a digital machine. It can only deal with analog via special tricks.

analogWrite (pin, value) - turns the pin ON and OFF very quickly which makes it act like an analog signal. This value is between 0 (0 volts) and 255(5 volts)

Page 17: UCF - Arduino - Lesson 2

Why are we doing this?(I told I was going to explain this later)

Remember that the Arduino is a digital machine. It can only deal with analog via special tricks.

analogREAD (pin, value) - This allows you to read the their voltage which outputs a value between 0 (0 volts) and 1024 (5 volts)

Page 18: UCF - Arduino - Lesson 2

The bottom line

When you READ a range from 0-1025 you must MAP and CONSTRAIN that range if you want to WRITE (0-255)it to a different device.

Therefore we READ the value from the PR. We then map and constrain it so that we can WRITE that value to the LED. The written value is then outputted as brightness in the LED.

Page 19: UCF - Arduino - Lesson 2

Programming our sketch

We first create a variable to READ the value of the PR

We then RE-MAP the ranges that the PR can produce to those same values we used to control the brightness of the LED. We also constrain these values to this range.

Lastly, we send the value (voltage) to the LED based on the LIGHTLEVEL of the PR.

Place your hand or finger on the PR to dim the LED.

Page 20: UCF - Arduino - Lesson 2

Challenge – Night LightInstead of lowering the LED brightness when

the light level on the PR is LOW, can you figure out a way to turn the LED ON when the value across the PR is LOW and OFF?

Hint: Use an “IF-ELSE” statement and other ideas from lesson 1 like math functions (+,-,*,/,<,>).

Write out what you WANT to do in WORDS first (psuedo-code) as this may help you determine how this is to be done.

YOU CAN DO THIS! And, YES……turn off the lights to test.