Carlotta Berry
Published

Lily∞Bot Raspberry PI Pico W: Photoresistor & LED

This project demonstrates how to use a photoresistor to control an LED on a Lily∞Bot with a Raspberry PI Pico W programmed in MIcroPython

BeginnerFull instructions provided1 hour100
Lily∞Bot Raspberry PI Pico W: Photoresistor & LED

Things used in this project

Hardware components

LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Photo resistor
Photo resistor
×1
Raspberry Pi Pico W
Raspberry Pi Pico W
×1
Lily Bot
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

MicroPython
MicroPython
Visual Studio 2017
Microsoft Visual Studio 2017
PlatformIO IDE
PlatformIO IDE

Story

Read more

Custom parts and enclosures

Lily Bot GIT HUB Repository

Lily Bot GIT HUB Repository

Schematics

Photoresistor and LED on Lily∞Bot

Photoresistor and LED on Lily∞Bot

Code

MicroPython code to control an LED with a photoresistor

MicroPython
#CAB 9.15.23 photo-led.py
#Use photoresistor to vary the LED brightness on the LilyBot
#LED on pin 27, photoresistor on pin 23
#https://www.noiresteminist.com/shop

from machine import Pin, ADC, PWM
from utime import sleep_us

#define inputs and outputs
frequency = 60
photoPin = 27
ledPin = 28
photo_val = ADC(Pin(photoPin))
led = PWM(Pin(ledPin))

# set frequency of pwm signal t o60 Hz
led.freq(frequency)
print("photoresistor-LED...")

while True:
    inmax = 65535
    inmin = 7000
    outmax = 65535
    outmin = 1000
    reading = photo_val.read_u16()
    #mapping function (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
    scale = int((reading-inmin)*(outmax-outmin)//(inmax-inmin))+outmin
    print("photo: " + str(scale))
    led.duty_u16(scale)
    #sleep_us(1)

LED with Photoresistor Code

MicroPython
#CAB 9.15.23 photo-led.py
#Use photoresistor to vary the LED brightness on the LilyBot
#LED on pin 27, photoresistor on pin 23
#https://www.noiresteminist.com/shop

from machine import Pin, ADC, PWM
from utime import sleep_us

#define inputs and outputs
frequency = 60
photoPin = 27
ledPin = 28
photo_val = ADC(Pin(photoPin))
led = PWM(Pin(ledPin))

# set frequency of pwm signal t o60 Hz
led.freq(frequency)
print("photoresistor-LED...")

while True:
    inmax = 65535
    inmin = 7000
    outmax = 65535
    outmin = 1000
    reading = photo_val.read_u16()
    #mapping function (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
    scale = int((reading-inmin)*(outmax-outmin)//(inmax-inmin))+outmin
    print("photo: " + str(scale))
    led.duty_u16(scale)
    #sleep_us(1)

Credits

Carlotta Berry

Carlotta Berry

13 projects • 19 followers
Carlotta Berry is a Professor and Dr. Lawrence J. Giacoletto Endowed Chair for Electrical and Computer Engineering at Rose-Hulman.

Comments