So this week we want to have an interactive animal. We want to use a light sensor and have it respond to different amount of lights to account for someone approaching and moving away from it. But…. it has to be trainable.
Well different rooms will have different brightness levels so we need a way to take in what the range will be in the user’s scenario
<aside> 💡 We had to figure out how to get reading from Light Sensor
</aside>
We were able to get the circuit right and used the light sensor and resistor to set it to one of our input pins and run this code to get values
import machine
import time
adc_pin = machine.Pin(28) # we need to set it to the pin itsplugged in
adc = machine.ADC(adc_pin)
while True:
reading = adc.read_u16()
print("ADC: ",reading)
time.sleep_ms(500)
When we get a reading we can simply take its position from our range:
$$ (input - minimum)/ range $$
We essentially turned the question into a simple math problem and we get numbers ranging from 0-180
Here is the GitHub to our full code:
Motor-Dance/Hunt Hoot Smarts at e22054b14ee1a7050ba6429eb74f6f03ddf71f28 · marcjm/Motor-Dance