Вы находитесь на странице: 1из 7

LIGHT SENSOR AND SERIAL PORT

Notebook: Robotics
Created: 4/16/2020 11:37 AM Updated: 4/19/2020 8:50 PM
Author: Criscela Ysabelle
Tags: homework

LIGHT SENSOR
phototransistor
also called
analog sensor
detects amount of light present
returns an ANALOG value
REVIEW FROM PREVIOUS LESSON: analog -
( a value in between the minimum
and maximum; unlike digital signals that only return 1 or 0)

USING THE LIGHT SENSOR


Changing the light intensity of an LED
The light sensor readings can be used to control the LED such that:
the LED darkens when the light sensor is covered, and
the LED emits greater light when the light sensor is uncovered and
exposed.
How it's done:
covering the light sensor returns a low reading value due to the
absence of light
the code then responds to the low reading by making the LED shine less
exposing the light sensor then returns a high reading value due to the
great amount of light
in turn, the code commands the LED to emit more light as well
Materials needed:
Control board (1)
Education Shield (1)
Light sensor module (1)
LED (1)
220 ohm resistor (1)
Module cable (1)
Jumper wires (2)
Assembly:
Attach the Education Shield to the Arduino Uno control board.
Connect the LED across the breadboard gap
REVIEW:
anode (positive, longer leg) should be on the DIGITAL side
(side nearer to pins 0 to 13)
cathode (negative, shorter leg) should be on the ANALOG
side (side nearer to pins A0 to A5)
Connect the 220 ohm resistor to digital pin #9.
Connect one leg of the resistor right next to where the
LED's positive leg is.
Connect the second leg diagonally,one row BELOW where
two
the LED's legs and resistor's first legs are; columns
ACROSS
It should be on the second column nearest to the
digital pins.
Use a jumper wire to connect it to pin #9.
Connect one side of the jumper wire right next to
the resistor's latter leg.
Connect the other to digital pin #9.
Connect the negative leg of the LED to GND on the other side using a
jumper wire.
Connect one side of the jumper wire on the same row as the LED
leg, last column (nearest to the analog pins).
Connect the other side to GND.
Connect the light sensor module to analog module connector A1.
Coding
Universal variables to declare:
int ledPin = indicates # of digital pin that LED is connected to (in
this case, 9)
int lightSensorPin = indicates # of analog pin that the light
sensor is connected to (A1)
Setup() code
none
Loop() code
Variables to declare:
int lightSensorValue (analog value of reading from the
light sensor)
int ledValue (analog value of how brightly the LED will be
illuminated)
Functions used
analogRead (lightSensorPin)
analogRead retrieves the analog value from a
sensor
returns any number from 0 to 1023
map (value, fromLow, fromHigh, toLow, toHigh)
proportionally re-maps a value from one range to
another
value = the value to be remapped
fromLow = the minimum of the initial range (in this
case, 0)
fromHigh = the maximum of the initial range (in this
case, 1023)
toLow = the new minimum of the re-map range (still
0)
toHigh = the new maximum of the re-map range (in
this case, 255)
this is done because analog values that can be
written 0 to 255
to a pin only range from .
analogWrite (ledPin, ledValue)
ledPin is the universal variable that dictates the pin
number that the LED is connected to.
ledValue is the remapped, returned value of how
brightly the LED should shine.
this function writes the ledValue to analog pin #9 -
the LED.
delay (int)
pauses the code for (int) milliseconds.
These variables and functions are under loop() because they will
continuously repeat.
The variables lightSensorValue and ledValue cannot be declared
alongside the universal variables because the values of these
are not constant and are subject to change as the program
repeats.
FULL PROGRAM CODE:

int ledPin=9;
int lightSensorPin=A1;

void setup()
{
//nothing here
}

void loop()
{
int lightSensorValue=analogRead(lightSensorPin);
int ledValue=map(lightSensorValue,0,1023,0,255);

analogWrite(ledPin, ledValue);
delay(10);
}
SENSOR CALIBRATION
sensors are ALWAYS dependent on environmental conditions
light sensors rely on the amount of light present in its current surroundings
are subject to change in different settings
THUS, sensors need to be calibrated when changing locations/conditions
(ex.) If your light sensor responds accordingly when in good lighting conditions, and
you suddenly transport it to a place with low light, your code will not work the way it
used to. The supposed reading for when your light sensor is covered, and for when it
isn't, will only be read as your light sensor ALWAYS being covered because your light
sensor is constantly in low light.
To adjust to this, you must calibrate your sensor to adapt to the new setting.
Calibration Example
Similar to the earlier example, the LED responds to the light sensor value.
It will turn on if the light sensor is covered, and turn off if it isn't.
If it doesn't work properly, turn the potentiometer knob until it does.
How it's done:
Similar to the first example, the activity of your LED will be reliant on
your light sensor's readings - except, this time, your LED will switch on
and off depending on the light sensor reading value.
A potentiometer will be used for calibration purposes.
Materials:
Same circuit from earlier
Potentiometer (1)
Jumper wires (3)
Assembly:

Connect the potentiometer to the side of the breadboard nearer to the


ANALOG pins.
Using a jumper wire, connect the middle pin of the potentiometer to the
analog pin A5.
Using another jumper wire, connect another pin on the potentiometer to
the IOREF pin,
Using the last jumper wire, connect the last potentiometer pin to the
row on the breadboard in front of GND, next to the jumper wire
connecting the LED to GND.
Coding
Universal variables to declare:
Same as earlier
int ledPin = 9;
lightSensorPin = A1;
int potPin = A5;
(indicates that the potentiometer is connected to pin A5).
Setup()
pinMode(pin, mode)
pin = pin number
mode = INPUT or OUTPUT
actual: pinMode(ledPin, OUTPUT)
configures the ledPin (pin 9) to act as an output
This is in setup because it only needs to be configured once.
Loop()
Variables to declare:
int lightSensorValue = same as earlier
int threshold = calibrates your sensor by continuously
reindicating the threshold based on environmental
conditions.
Functions used
analogRead (lightSensorPin)
reads an analog value returned by the light sensor.
analogRead (potPin)
reads an analog value from the potentiometer.
if
(lightSensorValue>threshold) { }
conditional statement
will execute the code inside the braces IF AND ONLY
IF the light sensor value is GREATER than the
threshold value returned by the potentiometer.
else
{}
conditional statement
if
code to be executed if the condition in is not
fulfilled (if the light sensor value is LESS than the
threshold)
digitalWrite (pin, value)
digitalWrite(ledPin, LOW)
if the light sensor value is GREATER than the
threshold, the LED will be turned OFF.
digitalWrite(ledPin, HIGH)
if the light sensor value is LESS than the
threshold, the LED will be turned ON.
(digitalWrite is used in lieu of analogWrite because
instead of having the LED shine at varying levels of
brightness, this program only either turns it ON or
OFF.)
delay
(int)
pauses the program for (int) milliseconds.
These variables and functions are under loop() because they will
continuously repeat.
The variables lightSensorValue and threshold cannot be declared
alongside the universal variables because the values of these
are not constant and are subject to change as the program
repeats.
FULL PROGRAM CODE:

int ledPin=9;
int lightSensorPin=A1;
int potPin=A5;

void setup()
{
pinMode(ledPin,OUTPUT);
}

void loop()
{
int lightSensorValue=analogRead(lightSensorPin);
int threshold=analogRead(potPin);

if(lightSensorValue>threshold)
{
digitalWrite(ledPin,LOW);
}
else
{
digitalWrite(ledPin,HIGH);
}

delay(10);
}

SERIAL PORT
enables a control board to communicate with a computer via USB connection
can be used to exchange complicated data w/computer
can send or receive text , instead of only digital or analog signals
can also communicate with OTHER softwares
(ex.) pressing a button on the board can cause the color of the screen in a
sketch (in the Processing software) to change
the board sends the data re.: the state of the button
(pressed/unpressed)
the computer receives the data and responds to it by changing
the color
uses the DIGITAL 0
pins and 1.
0
Pin : RX or receiver; the board RECEIVES data, or
1
Pin : TX or transmitter; the board SENDS the data.

(The TX and RX pins are shown above.)


functions digitalRead() and digitalWrite() should NOT be used when using serial
communication.
sender and receiver should communicate at the same speed.
baud rate
term used for communication speed
measured at bits per second, similar to internet connection speeds measured
in kbps (kilobytes per second).
COMMON SPEED (when using an Arduino control board with a computer running
the Processing software) = 9600 bits per second.

FOOTNOTE:
Hello Miss! Sorry po if the layouting (indentation, etc.) is a bit weird; I made this file with
Evernote. I tried to transfer it to Word after I finished, but it looked even worse.

Thank you po, and I hope you and your family stay safe!

Best regards,
Criscela Ysabelle

Вам также может понравиться