0% found this document useful (0 votes)
101 views20 pages

Light Detection and Measurement: Dr. Eng. Endrowednes Kuantama

The document discusses reading analog sensor values using an Arduino. It explains that the Arduino contains an analog-to-digital converter that can read input voltages between 0-5V and map them to integer values between 0-1023. It provides code examples to read the value of a light dependent resistor (LDR) sensor using analogRead() and use the value to control the brightness of an LED with analogWrite(). The document also discusses using analog sensor readings as input to control the speed of a servo motor.

Uploaded by

Aman Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views20 pages

Light Detection and Measurement: Dr. Eng. Endrowednes Kuantama

The document discusses reading analog sensor values using an Arduino. It explains that the Arduino contains an analog-to-digital converter that can read input voltages between 0-5V and map them to integer values between 0-1023. It provides code examples to read the value of a light dependent resistor (LDR) sensor using analogRead() and use the value to control the brightness of an LED with analogWrite(). The document also discusses using analog sensor readings as input to control the speed of a servo motor.

Uploaded by

Aman Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

ANALOG – READ/WRITE LIGHT DEPENDENT RESISTOR (LDR)

ARDUINO CODING
SENSOR

LIGHT DETECTION AND MEASUREMENT


Dr. Eng. Endrowednes Kuantama
ARDUINO CODING
O P E R AT O R S
ARDUINO CODING
O P E R AT O R S
ARDUINO CODING
O P E R AT O R S
ANALOG – READ/WRITE
Analog Read
analogRead (pin,value) Potentiometer
•Just as we would like to read the state of simple on/off switches, we also
analogRead()
need to read continuously variable (i.e. analog) data.
•Usually this means the output voltage caused by some form of sensor such
as a temperature sensor, force sensor, light sensor, etc.
•The position of the pot would be controlled by the user and could represent
a setting of almost any conceivable parameter such as volume, brightness,
time delay, frequency, etc.
• In order to read analog quantities, the ATmega 328p contains a single 10 bit
analog-to-digital converter multiplexed across six input channels.
• The Arduino development environment contains two useful functions to
access theses, namely analogRead() and analogReference().
• Reads the value from the specified analog pin.
• The Arduino board contains a 6 channel (8 channels on the Mini and
Nano, 16 on the Mega), 10-bit analog to digital converter. This means
that it will map input voltages between 0 and 5 volts into integer
values between 0 and 1023.
• This yields a resolution between readings of: 5 volts / 1024 units or,
.0049 volts (4.9 mV) per unit.
• The input range and resolution can be changed using
analogReference().
• It takes about 100 microseconds (0.0001 s) to read an analog input, so
the maximum reading rate is about 10,000 times a second.
ARDUINO CODING

#define ledPin 13
#define potPin // select the input pin for the potentiometer

int val;

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}

void loop() {
val = analogRead(potPin); // read the value from the sensor

digitalWrite(ledPin, HIGH); // turn the ledPin on


delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}
ARDUINO CODING Analog Write
analogWrite (pin,value) Potentiometer
•While some more advanced microcontrollers analogRead()
contain a digital to analog
converter (DAC) to produce a continuously variable analog output
signal, most do not.
•The DAC (along with a low-pass reconstruction filter) reconstructs
these data into a smooth signal.
•The second method relies on pulse width modulation. This scheme is
employed on the Uno and many other Arduino boards.
•Note that not all loads will operate properly with a simple PWM signal.
Some loads may require further processing of the PWM signal (such as
filtering).
analogWrite()

Description
• Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying
brightnesses or drive a motor at various speeds.
• After a call to analogWrite(), the pin will generate a steady square wave of the specified
duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on
the same pin). The frequency of the PWM signal is approximately 490 Hz.
• On most Arduino boards (those with the ATmega168 or ATmega328), this function works on
pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older
Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
• The Arduino Due supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1.
Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true
analog outputs.
The bottom line is that you call analogWrite()
with just the pin of interest and a duty cycle
value that ranges from 0 (off) to 255 (fully on).
ARDUINO CODING

//******************************************************************//analog input pins


#define viewer_sensor A5 //analog pin - left IR sensor
//digital output pins
#define illumination_output 0 //illumination output pin

unsigned int viewer_sensor_reading; //current value of sensor output

void setup()
{
pinMode(illumination_output, OUTPUT); //config. pin 0 for dig. output
}

void loop() //read analog output from IR sensor


{
viewer_sensor_reading = analogRead(viewer_sensor);
}
ARDUINO CODING
LOOPING
Knight Rider LED

❑ This example makes use of 6 LEDs


connected to the pins 2 - 7 on the board
using 220 Ohm resistors.
❑ The first code example will make the LEDs
blink in a sequence, one by one using only
digitalWrite(pinNum,HIGH/LOW) and
delay(time).
❑ The second example shows how to use a
for(;;) construction to perform the very
same thing, but in fewer lines.
❑ The third and last example concentrates in
the visual effect of turning the LEDs on/off
in a more softer way.
int timer = 100; // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)
int i;

void setup()
{
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output
}

void loop()
{
for (i = 0; i < num_pins; i++)
{ // loop through each pin...
digitalWrite(pins[i], HIGH); // turning it on,
delay(timer); // pausing,
digitalWrite(pins[i], LOW); // and turning it off.
}

for (i = num_pins - 1; i >= 0; i--)


{
digitalWrite(pins[i], HIGH);
delay(timer);
digitalWrite(pins[i], LOW);
}
}
ARDUINO CODING

Fading - Demonstrates the use of analog output (PWM) to fade an LED.


Circuit - An LED connected to digital pin 9.
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
void setup()
{
// nothing for setup
}
void loop()
{
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
}
}
SERVO

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy