from machine import Pin
from neopixel import NeoPixel
import onewire
import ds18x20
import time
pin = Pin(13, Pin.OUT) # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels
np[0] = (255, 5, 5) # set the first pixel to white with low intensity
np[1] = (255, 0, 0) # set the second pixel to red with low intensity
np[2] = (0, 255, 0) # set the third pixel to green with low intensity
np[3] = (0, 0, 255) # set the fourth pixel to blue with low intensity
np[4] = (255, 255, 0) # set the fifth pixel to yello with low intensity
np[5] = (85, 85, 0)
np.write()
# Define the pin where the DS18B20 data line is connected
data_pin = Pin(26, Pin.PULL_UP) # Replace with your GPIO pin
# Set up the 1-Wire bus
ow = onewire.OneWire(data_pin)
# Set up the DS18B20 sensor
ds = ds18x20.DS18X20(ow)
# Scan for devices on the 1-Wire bus
devices = ow.scan()
if not devices:
print("No DS18B20 devices found on the bus!")
else:
print("Found DS18B20 devices:", devices)
while devices:
try:
# Initiate temperature conversion
ds.convert_temp()
time.sleep(1) # Wait for the conversion to complete
for device in devices:
# Read and print the temperature for each device
temp = ds.read_temp(device)
print("Temperature: ",temp,"°C")
if temp > 26:
for x in range(8):
np[x] = (255,0,0 )
else:
for x in range(8):
np[x] = (0,255,0)
np.write()
time.sleep(2) # Delay before the next reading
except Exception as e:
print("Error reading temperature:", e)
# Ontwerp een klein programma voor je ESP32 in micropython dat:
# 1/ de temperatuur meet met de DS18B20 en deze print in je terminal
# 2/ de ledpixelstrip groen kleurt indien de temp < 26 graden C is en rood kleurt bij temperaturen boven de 26 graden C.
# Je kan door je vinger op de DS18B20 te leggen de temperatuur beïnvloeden en zo je programma testen.
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ds18b20
ds18b20