← Micro:bit

Chapter 7

Reading Analog Signals

อ่านค่าสัญญาณแอนะล็อก

  • Both micro:bit V1 and V2 can read analog signals (สัญญาณแอนะล็อก) on 6 pins: P0, P1, P2, P3, P4 and P10.
  • A built-in converter turns the voltage into a number with 10-bit resolution.
  • The number runs from 0 to 1023, for an input voltage of 0 to 3.3 V.

7.1 10-Bit ADC Architecture อ่านค่าแอนะล็อกผ่านขาพอร์ต P0, P1 และ P2

  • An Analog-to-Digital Converter (ADC) (วงจรแปลงสัญญาณแอนะล็อกเป็นดิจิทัล) measures a voltage.
  • It gives back a whole number that your code can use.
Analog: any value 3.3 V 0 V Digital: only 2 levels 3.3 V (1) 0 V (0) light level, battery, volume knob button pressed or not
Digital has only two levels. Analog can be any value in between, and it changes smoothly.
1. Smooth voltage + samples ADC 2. Whole numbers (steps) continuous 1024 possible levels (0–1023)
The ADC takes a sample at regular moments and rounds each one to the nearest level. The result is a staircase of numbers.

The scale: number against voltage

ADC count 1 step ≈ 3.2 mV Voltage on the pin 0 0 V 256 0.83 V 512 1.65 V 768 2.48 V 1023 3.3 V
Every ADC number matches one voltage. 0 is 0 V and 1023 is 3.3 V. Everything in between is spread evenly.
  • 10 bits give 210 = 1024 levels, so the numbers are 0 to 1023.
  • Smallest step: 3.3 V ÷ 1023 ≈ 3.2 mV.
  • Halfway (512) is about 1.65 V.
Analog Property Technical Specification (ข้อกำหนดทางเทคนิค)
ADC Resolution 10-bit: 210 = 1024 levels (numbers 0 to 1023)
Input Voltage Range 0 V to 3.3 V (0 V = 0, 3.3 V = 1023)
Step Resolution 3.3 V ÷ 1023 ≈ 3.2 millivolts (mV) per count
Analog Input Pins 6 pins: P0, P1, P2, P3, P4 and P10
Write-Only (PWM) Pins 11 pins: P5, P6, P7, P8, P9, P11, P12, P13, P14, P15, P16

Where the pins are ตำแหน่งขา P0, P1, P2

P0 P1 P2 3V GND Analog input: P0, P1, P2 (big rings) and also P3, P4, P10 (small gold pads)
P0, P1 and P2 are big rings on the edge connector, so crocodile clips can grip them. 3V and GND are the other two rings.

Safe voltages

1.2–1.5 V AA / AAA cell SAFE ✔ 3.3 V the maximum LIMIT 5, 9, 12 V too high DAMAGE ✖ Mains wall socket NEVER ✖
The pins can take 3.3 V at most. A normal AA or AAA cell is fine.

⚠️ Be careful: you can destroy the board

  • The micro:bit's circuits accept 3.3 V at most.
  • Never connect 5 V, 9 V, 12 V or mains power to P0, P1 or P2. The chip burns out for good.
  • AA or AAA cells (1.5 V alkaline, 1.2 V rechargeable) are under the limit, so you can read them directly.

MakeCode block: analog read pin [P0]

Pin P0 0 – 3.3 V 10-bit ADC built into the chip analog read pin 0 – 1023 show number
The block hands your program the ADC count from the chosen pin: a number from 0 to 1023.
  • It returns a digital number from 0 to 1023 for the pin you choose.
  • Try it: move the "Voltage on P0" slider and watch the count change.

💻 7.1 Reading the raw ADC count on pin P0

forever
show number analog read pin P0
pause (ms) 1000
See it in JavaScript
basic.forever(function () {
	basic.showNumber(pins.analogReadPin(AnalogPin.P0))
	basic.pause(1000)
})

Next: 7.2 DIY Battery Voltage Tester Project →

7.2 DIY Battery Tester Project ทดสอบแรงดันไฟฟ้าแบตเตอรี่

  • An alkaline or rechargeable AA or AAA cell gives about 1.2 to 1.5 V.
  • That is under the micro:bit's 3.3 V limit, so the board can read it directly.
LOW: below 1.2 V GOOD 1.2 V = count 372 1.5 V = 465 0 V = 0 New alkaline cell: about 1.5 V. Worn out: under 1.2 V.
What a good and a worn cell look like to the ADC. The line at 1.2 V (count 372) splits good from low.

Physical wiring setup การต่อวงจร

  • You need only 2 things: a micro:bit V2 and 2 crocodile clips.
P0 P1 P2 3V GND AA battery 1.5 V + − Display tick or cross shows here (Yes / No icon) Red clip P0 to battery + Black clip GND to battery −
Red clip: battery + to P0. Black clip: battery − to GND. The LED matrix shows the verdict.
  1. Step 1 — Red clip (positive): clip the P0 ring to the + end of the battery.
  2. Step 2 — Black clip (ground): clip the GND ring to the − end of the battery.
  3. Step 3 — Power the board: use a micro USB cable to a computer, or a 3 V battery pack on the JST connector at the back.
1.5 V good cell 0.9 V low cell
Result on the LED matrix: a tick for a good cell, a cross for a low cell. These are the real Yes and No icons of the show icon block.

Next: Voltage Math & Full Program →

Voltage Calculation Formula & Program สูตรการคำนวณและโค้ดการทำงาน

Formula for the measured voltage:

voltage = (ADC count × supply voltage of the micro:bit) ÷ maximum ADC count

V = (ADC × 3.3) ÷ 1023

3.3 V 1023 0 ADC count (0 – 1023) Voltage (V) 1.2 V at 372 1.5 V at 465
The formula is a straight line: double the ADC count and you double the voltage.

Worked example ตัวอย่างการคำนวณ

  • The analog read block gives 465. What is the voltage, and is the battery good?
  1. Step 1 — Multiply: 465 × 3.3 = 1534.5
  2. Step 2 — Divide: 1534.5 ÷ 1023 = 1.5 V
  3. Step 3 — Compare: 1.5 V is 1.2 V or more, so the display shows the tick.
ADC count 465 × 3.3 1534.5 ÷ 1023 1.5 V 1.2 V or more ✔ GOOD ADC count 310 × 3.3 1023 ÷ 1023 1.0 V below 1.2 V ✖ LOW
The same three steps for two batteries. A count of 310 gives exactly 1.0 V, which is below 1.2 V.

Evaluation criteria การประเมินสภาพแบตเตอรี่

  • 1.2 V or more: the battery still has power → show icon (Yes).
  • Below 1.2 V: the battery is worn out → show icon (No).
Press button A analog read pin P0 → count V = count × 3.3 ÷ 1023 V ≥ 1.2 V ? Yes No show icon Yes show icon No
The decision the program makes. In MakeCode the if block tests Voltage < 1.2 and puts the cross first; the result is the same.
Yes: 1.2 V or more
No: below 1.2 V

The textbook program การทำงานของโปรแกรม

on start show the text "Battery Tester" on button B pressed show the raw ADC count (0–1023, not yet good or bad) on button A pressed 1. read P0, convert to volts 2. scroll the volts 3. tick (1.2 V or more) or cross (below 1.2 V)
Three events: start-up, button B and button A.
  • on start: show the text "Battery Tester".
  • Button B: show the raw ADC count. It does not yet tell you good or bad.
  • Button A: read P0, convert to volts and scroll the voltage.
  • Then show a tick if the voltage is 1.2 V or more, or a cross if it is lower.
  • The voltage scrolls with all its decimals (like 1.45161290323), so the tick or cross comes a moment later.

💻 7.2 Battery tester — count on B, volts and verdict on A

on start
show string Battery Tester
on button B pressed
show number analog read pin P0
on button A pressed
set Voltage to (analog read pin P0 × 3.3) / 1023
show number Voltage
if Voltage < 1.2 then
show icon
else
show icon
See it in JavaScript
let Voltage = 0
basic.showString("Battery Tester")
input.onButtonPressed(Button.B, function () {
	basic.showNumber(pins.analogReadPin(AnalogPin.P0))
})
input.onButtonPressed(Button.A, function () {
	Voltage = pins.analogReadPin(AnalogPin.P0) * 3.3 / 1023
	basic.showNumber(Voltage)
	if (Voltage < 1.2) {
		basic.showIcon(IconNames.No)
	} else {
		basic.showIcon(IconNames.Yes)
	}
})

Next: Inspect the Real Physical Setup →

Real Physical Battery Tester Hardware Setup

  • This photo shows a real micro:bit V2 wired to an alkaline AA battery with crocodile clips.
  • Hover over or tap each part to see its physical and electrical role.
Battery Clips P0 + GND ADC Program LEDs
The signal path from the battery to the screen, in the same order as the photo.
🔍

Real micro:bit V2 Battery Voltage Tester Setup

Interactive Map
Real micro:bit V2 Battery Tester Physical Setup
👉

Hover or tap the 5x5 LED Matrix, P0 (+) Clip, GND (-) Clip, 1.5V Battery, or Terminals to inspect the physical testing circuit.

Next: Test Your Knowledge with the Quiz →

Chapter 7 Review & Self-Check

  • Check your understanding of the 10-bit ADC, safe voltages, the voltage formula and battery checks.
10-bit ADC 0 – 1023 = 0 – 3.3 V Formula V = ADC × 3.3 ÷ 1023 Verdict at 1.2 V
Chapter 7 in one picture.
1. What is the digital output range of the 10-bit ADC when reading an analog pin on the micro:bit?
2. What is the maximum safe input voltage that can be applied to micro:bit analog pins (P0, P1, P2)?
3. Which formula correctly converts a raw ADC integer reading into an actual voltage in Volts?
4. How should crocodile clips be connected when testing a 1.5V AA/AAA battery with micro:bit?
5. In the battery tester, which voltage separates a healthy cell from a flat one?
6. Which pins on the micro:bit can read an analog voltage?
7. A reading of 1023 on pin P0 stands for which voltage?
8. In the battery tester, which clip goes to the + end of the battery?