Skip to main content

2.3. Photoresistor Analog Read

Under construction
Short example demonstration video

Along with reading digital inputs, we can also measure analog values using the built-in ADC (Analog-to-Digital Converter) on the NULA Mini board. While digital inputs can only detect two states (HIGH or LOW), analog inputs represent a continuous range of values. In this example we'll use a photoresistor (LDR) to measure the intensity of light which then changes its resistance. NULA Mini by default converts the voltage (ranging from 0V to 3.3V) into a number between 0 - 4095 (12-bit ADC).

At the end of this example you will learn:

  • How to use a photoresistor
  • How to read analog values
ℹ️

Parts required:

  • Soldered NULA Mini Board
  • Breadboard
  • 1 x Photoresistor
  • 1 x 10kΩ resistor
  • Some jumper wires
Under construction
Required components

Putting the components together

  • Place each leg of the photoresistor in a different row on the breadboard
  • Connect one leg to 3.3V and the other end to the ADC capable pin on your board (IO5). From that same row then connect 10kΩ resistor between IO5 and GND.
Under construction
Components connected together

Code Example

Along with Pin module we import the ADC which allows us to create the object of that type which will be used to read analog values from a pin.

from machine import Pin, ADC
import time

Define pin used for LDR sensor and create an ADC object for that pin.

LDR_PIN = 5
ldr = ADC(Pin(LDR_PIN))

Main loop which reads the analog value from that pin and prints it over to serial.

while True:
# Read the analog value from the photoresistor
ldr_value = ldr.read()

# Print the LDR value to the console
print(f"LDR Value: {ldr_value}")

# Pause the program briefly to allow some time for CPU
time.sleep(0.5)

Full Example

2.3_Photoresistor_Analog_Read.py