1

Im' trying to create my own Library for the Neopixel Led strips, because there is no Library with the functions I need and I'm wondering how the LEDs work. In the Datasheet of the LEDs I found out that I have to send 24 Bit GRB color values in form of a square wave an make a pause of 50us between ever 24 bit.

Bit https://i.stack.imgur.com/SG30f.png

Period Times Of The Waves period times of the square wave

Now I'm wondering how I can translate those 24 bit GRB colors to a square wave with the help of a raspberry Pi.

Example01

import time
import RPi.GPIO as GPIO



us = 0
GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.OUT)

def microdelay(us):
    time.sleep(us/1e6)




def zero():
    GPIO.output(18, GPIO.HIGH)
    microdelay(0.4)
    GPIO.output(18, GPIO.LOW)
    microdelay(0.85)

def one():
    GPIO.output(18, GPIO.HIGH)
    microdelay(0.8)
    GPIO.output(18, GPIO.LOW)
    microdelay(0.45)


# Color Red : 0000 0000 1111 1111 0000 0000

if __name__ == "__main__":
    zero()
    zero()
    zero()
    zero()

    zero()
    zero()
    zero()
    zero()

    one()
    one()
    one()
    one()

    one()
    one()
    one()
    one()

    zero()
    zero()
    zero()
    zero()

    zero()
    zero()
    zero()
    zero()
    print("strip is red")

i get this output on console:

pi@raspberrypi:~/Libary testing $ sudo python3 LED_testing.py

LED_testing.py:9: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(18, GPIO.OUT)

strip is red

pi@raspberrypi:~/Libary testing $

example02

import time


import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.OUT)

def usleep(us: int):
  time.sleep(us/1e6)


def one():
    GPIO.output(18, GPIO.HIGH)
    usleep(0.8)
    GPIO.output(18, GPIO.LOW)
    usleep(0.45)



def zero():
    GPIO.output(18, GPIO.HIGH)
    usleep(0.4)
    GPIO.output(18, GPIO.LOW)
    usleep(0.85)




if __name__ == '__main__':

    for i in range(0, 862):
        # Green
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        # Red
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        # Blue
        one()
        one()
        one()
        one()

        one()
        one()
        one()
        one()

        usleep(50)
    print("strip is white")

console output: pi@raspberrypi:~/Libary testing $ sudo python3 LED_testing.py

LED_testing.py:7: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(18, GPIO.OUT) strip is white

pi@raspberrypi:~/Libary testing $

led_pat
  • 21
  • 3

1 Answers1

0

a squarewave is just a graphical representation of a digital signal. Following table is important:

enter image description here

When you then look at the sequence chart:

enter image description here

You can see the following:

  • A 0 is represented by 0.4us High followed by 0.85us low
  • A 1 is represented bu 0.8us High followed by 0.45us low
  • A rest is a low signal for 50us or more

Hint: To sleep for 0.4 ms you could do the following:

import time

# sleep for 0.4us
time.sleep(0.4/1000000)

let's say you want to send the GBR value (255, 16, 12). that would translate in: 11111111001000000001100

in python, this could be done with bit operations:

def get_bits_from_grb(g: int, r: int, b: int) -> list:
  return list(bin(g<<16|r<<8|b).replace("0b", "")) 

Documentation states: Follow the order of GRB to sent data and the high bit sent at first.

'high bit sent first' means we send from left to right.

your code would be something among the lines of:

import time

def usleep(us: int):
  time.sleep(us/1e6)

def send_bit(signal_bit: bool):
  if signal_bit:
    GPIO.output(pin_number, GPIO.HIGH)
    usleep(0.8)
    GPIO.output(pin_number, GPIO.LOW)
    usleep(0.45)
  else:
    GPIO.output(pin_number, GPIO.HIGH)
    usleep(0.4)
    GPIO.output(pin_number, GPIO.LOW)
    usleep(0.85)

def send_rest():
  GPIO.output(pin_number, GPIO.HIGH)
  usleep(0.51) # should be more than 50us
  GPIO.output(pin_number, GPIO.LOW)

def send_bits(bit_list: list):
  for b in bit_list:
    send_bit(b)

bit_list = get_bits_from_grb(176, 106, 76)
send_bits(bit_list)
Dr. Casual
  • 418
  • 3
  • 10
  • Thanks for your answer. I could do it that way. But i would like to give an input in terminal with the RGB values from 0-255, then convet it to binary and then i want to convert the 24Bit to the squarewave. For a 0 0.4us High and for 0.85us low and for 1 0.8s High and 0.45us Low. – led_pat Mar 22 '21 at 18:16
  • Computers don't know anything else but binary ;) You should use bit operations for that. Let's say you've got a GRB value of (255, 16, 12) then you should do 255 << 16 | 16 << 8 | 12. Then to create you list of values: list(bin(255<<16|12<<8|12).replace("0b", "")) – Dr. Casual Mar 22 '21 at 18:28
  • and how do i convert the binary to the square wave? – led_pat Mar 22 '21 at 18:40
  • By setting the GPIO pin high and low for the correct timings – Dr. Casual Mar 22 '21 at 18:46
  • I try that thank you for your fast answers. – led_pat Mar 22 '21 at 20:29
  • I've written some code. I've added it to the question as testcode01. The code doesn't work and i don't know why. It would be nice if you could take a look for mistakes. – led_pat Mar 23 '21 at 20:14
  • I don't have a neopixel,so can't test it myself. Could you try adding a rest after it and placing the code in a loop? – Dr. Casual Mar 23 '21 at 20:35
  • I've added a for in range loop and now the whole strip should light up white, but i does nothing. The Code is Example02 in Question. – led_pat Mar 23 '21 at 20:46
  • yes, i give the command: sudo python3 LED_testing.py – led_pat Mar 23 '21 at 20:57
  • hmmm I'm guessing you would need PWM then – Dr. Casual Mar 23 '21 at 21:19
  • got discord? Might be a little bit easier – Dr. Casual Mar 23 '21 at 21:23
  • yeah i have discord. Please send me your Discord information to this email: o339r7+b5nysvn5xubow@sharklasers.com this email is valid for 1h – led_pat Mar 23 '21 at 22:00
  • If you already sent the information, i didnt get them. Could you please send it to: led.pat.neopix@gmail.com. – led_pat Mar 23 '21 at 22:25