🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 9. Sensing Light
  • Overview
  • Parts
  • Breadboard Layout
  • Photocells
  • Arduino Code
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 9. Sensing Light

PreviousLesson 8. Analog InputsNextLesson 10 Making Sounds

Last updated 1 year ago

Arduino Lesson 9. Sensing Light

Created by Simon Monk

https://learn.adafruit.com/adafruit-arduino-lesson-9-sensing-light

Last updated on 2022-12-01 01:52:48 PM EST

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

Breadboard Layout 9

Photocells 10

Arduino Code 11

Other Things to Do 12

Overview

In this lesson, you will learn how to measure light intensity using an Analog Input. You will build on lesson 8 and use the level of light to control the number of LEDs to be lit.

The photocell is at the bottom of the breadboard, where the pot was in lesson 8.

Parts

5mm red LED 8

1 kΩ Resistor (brown, black, red stripes) 1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack

1

Breadboard Layout

The breadboard layout for this lesson is the same as for lesson 8, except that the pot is replaced by an LDR and a 1 kΩ resistor.

Here is the layout for lesson 8, near the pot.

Simply remove the pot and replace it with the photocell and resistor as shown below:

Photocells

The photocell used is of a type called a light dependent resistor, sometimes called an LDR. As the name suggests, these components act just like a resistor, except that the resistance changes in response to how much light is falling on them.

This one has a resistance of about 50 kΩ in near darkness and 500 Ω in bright light. To convert this varying value of resistance into something we can measure on an Arduino's analog input, it need to be converted into a voltage.

The simplest way to do that is to combine it with a fixed resistor.

The resistor and photocell together behave rather like a pot. When the light is very bright, then the resistance of the photocell is very low compared with the fixed value resistor, and so it is as if the pot were turned to maximum.

When the photocell is in dull light the resistance becomes greater than the fixed 1kΩ resistor and it is as if the pot were being turned towards GND.

Load up the sketch given in the next section and try covering the photocell with your finger, and holding it near a light source.

Arduino Code

The sketch that you used in lesson 8, will work, but you will find that you will not be able to find a light bright enough to light all the LEDs. This is because of the fixed resistor, so we need to compensate for the fact that no matter how low the resistance of the photocell falls, there will always be the 1 kΩ of the fixed resistor offsetting it.

The slightly modified lesson 8 sketch is listed below:

/*
Adafruit Arduino - Lesson 9. Light sensing
*/
int lightPin = 0; 
int latchPin = 5; 
int clockPin = 6; 
int dataPin = 4;
int leds = 0; 
void setup()
{
    pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT);
}
void loop()
{
    int reading = analogRead(lightPin);
    int numLEDSLit = reading / 57; //1023 / 9 / 2 if (numLEDSLit > 8) numLEDSLit = 8;
    leds = 0; // no LEDs lit to start
    for (int i = 0; i < numLEDSLit; i++)
    {
        leds = leds + (1 << i); // sets the i'th bit
    }
    updateShiftRegister();
}
void updateShiftRegister()
{
    digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, leds); digitalWrite(latchPin, HIGH);
}

The first thing to note is that we have changed the name of the analog pin to be 'lightPin' rather than 'potPin' since we no longer have a pot connected.

The only other substantial change to the sketch is the line that calculate how many of the LEDs to light:

int numLEDSLit = reading / 57; // all LEDs lit at 1k

This time we divide the raw reading by 57 rather than 114 from lesson 8. In other words we divide it by half as much as we did with the pot, to split it into nine zones, from no LEDs lit, to all eight lit. This extra factor is to account for the fixed 1 kΩ resistor. This means that when the photocell has a resistance of 1 kΩ (the same as the fixed resistor) the raw reading will be 1023 / 2 = 511. This will equate to all the LEDs being lit and then a bit (numLEDSLit will be 9).

Other Things to Do

To vary the sensitivity of the light meter you have just made, try changing the value of the factor (57) that you divide the analog reading by.

Increasing this value will make the reading less sensitive.

Another thing that you could try is to change the sketch so that while the reading is below a certain level, the LEDs are on, but they automatically turn off when a threshold is exceeded. So putting your finger near the photocell to cut out most of the light will turn on the LEDs.

Part Qty

270 Ω Resistors (red, purple, brown stripes) 8

74HC595 Shift Register 1

Photocell (Light Dependent Resistor) 1