🔬
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 8. Analog Inputs
  • Overview
  • Parts
  • An Experiment
  • Variable Resistors (Pots)
  • Breadboard Layout
  • Arduino Code
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 8. Analog Inputs

Test Analog input

PreviousLesson 7. Make an RGB LED FaderNextLesson 9. Sensing Light

Last updated 1 year ago

Arduino Lesson 8. Analog Inputs

Created by Simon Monk

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

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

An Experiment 8

Variable Resistors (Pots) 10

Breadboard Layout 11

Arduino Code 12

Other Things to Do 13

Overview

In this lesson, you will start by using the Serial Monitor to display analog readings, and then extend the project using eight LEDs from lesson 4, so that you can control the number of LEDs that are lit by turning the knob on a variable resistor.

Parts

To build the projects described in this lesson, you will need the following parts.

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

10 kΩ variable resistor (pot) 1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack

1

An Experiment

Before we go ahead and use the LEDs, you can try a little experiment using just the variable resistor also known as a potentiometer (often called a 'pot' for short) and the Arduino Serial Monitor.

Connect up your breadboard as shown below:

Load the following sketch onto your Arduino.

/*

Adafruit Arduino - Lesson 8. Analog Inputs

*/

int potPin = 0; void setup()

{

Serial.begin(9600);

}

void loop()

{

int reading = analogRead(potPin); Serial.println(reading); delay(500);

}

Now open the Serial Monitor, and you will see a stream of numbers appearing.

Turn the knob on the variable resistor and you will see the number change between 0 and 1023.

The Serial Monitor is displaying the analog reading value from A0 using the line:

int reading = analogRead(potPin);

The voltage at A0 is being transformed into a number between 0 and 1023.

Variable Resistors (Pots)

For historical reasons, variable resistors are often called 'pots' which is short for 'potentiometers'.

In our experiment with the Serial Monitor, the pot is somehow varying the voltage at A0 and the little test sketch is converting this voltage into a number between 0 and 1023.

Your pot has a circular 'track' that acts as a resistor, in our case it's a 10 kΩ resistor. However, the difference with a pot, is that there is also a middle connection called the 'slider'. This connection is rotated when you turn the pot. So if you connect one end of the pot to 5V and the other to GND, then the voltage at the slider will vary between 0 and 5V as you turn it.

Breadboard Layout

Let's do something more interesting with the pot. We can use it to control the number of LEDs lit.

This breadboard layout is based on that of lesson 4, there are a few jumpers moved, and the pot and it's connections to the Arduino have been added.

Arduino Code

Load the following sketch onto your Arduino board.

/*

Adafruit Arduino - Lesson 8. Analog Inputs - LEDs

*/

int potPin = 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(potPin);

int numLEDSLit = reading / 114; //1023 / 9 leds = 0;

for (int i = 0; i < numLEDSLit; i++)

{

bitSet(leds, i);

}

updateShiftRegister();

}

void updateShiftRegister()

{

digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, leds); digitalWrite(latchPin, HIGH);

}

You should recognize much of this code from lesson 4. So refer back to that lesson for more information about how the LEDs are controlled.

The key parts of the sketch as far as analog inputs are concerned are the line where we define the analog pin that we are going to connect to the slider of the pot:

int potPin = 0;

Note that we do not need to put anything in 'setup' to set the pin mode for an analog input.

In the main loop, we read the analog value like this:

int reading = analogRead(potPin);

But then this reading between 0 and 1023 needs converting into a number of LEDs to light, between 0 and 8. The range of numbers, between 0 and 8 is actually 9 values. So we need to scale the reading by 1023 divided by 9 or 114.

int numLEDSLit = reading / 114;

To light the right number of LEDs, we use the 'for' loop to count from 0 up to 'numLEDSLit' setting the bit at that position.

leds = 0;

for (int i = 0; i < numLEDSLit; i++)

{

bitSet(leds, i);

}

Finally we update the shift register with a call to:

updateShiftRegister();

Other Things to Do

It is actually simpler to light a single LED to indicate the position of the knob. Try modifying your sketch to do this.

Part Qty

5mm red LED 8

74HC595 Shift Register 1