Skip to content

Instantly share code, notes, and snippets.

@gustavolaureano
Created August 4, 2023 20:15
Show Gist options
  • Save gustavolaureano/2a22a55951e6de8192e117d560d261e3 to your computer and use it in GitHub Desktop.
Save gustavolaureano/2a22a55951e6de8192e117d560d261e3 to your computer and use it in GitHub Desktop.
MicroPython code for the raspberry pico to count how many WS2812 are connected, the data signal of the LEDs must be connected to GP2 and the ADC (GP26) must be connected to the strip ground after a jump wire (I used a cheap 20cm jumper cable), using the jumper as a shunt
import machine, neopixel, time
from machine import Pin, ADC
np = neopixel.NeoPixel(Pin(2), 200)
adcpin = machine.ADC(26)
while True:
num_of_leds = None
for i in range(len(np)):
onvalue = 0
offvalue = 0
for t in range(10):
np[i] = (255, 255, 255)
np.write()
onvalue += adcpin.read_u16()
np[i] = (0, 0, 0)
np.write()
offvalue += adcpin.read_u16()
dist = abs(onvalue - offvalue)
#print(f"{i+1} on: {onvalue} off: {offvalue} dist: {dist}")
if (dist < 1100):
num_of_leds = i
break
if (num_of_leds):
print(f"Found: {num_of_leds}")
else:
print(f"No LEDs found.")
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment