Micropython chained shift registers don't behave as expected

414 views Asked by At

I'm new to electronics and have been having fun with an esp8266 and Micropython. I'm trying to get a couple of chained shift registers (sn74hc595) working to control 16 leds and am struggling to understand the results I'm getting.

My script is as follows:

from machine import Pin
import time

def sixteen_bit(sleep_time=100):
    while True:
        for value in [1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535]:
            bits = [value >> i & 1 for i in range(15, -1, -1)]
            for i in range(15, -1, -1):
                data.value(bits[i])
                clock.value(1)
                clock.value(0)
            latch.value(1)
            latch.value(0)

            time.sleep_ms(sleep_time)

It's a bit hard to tell in my video but each led lights up individually before it all repeats. Google Drive Video Example

Based on my code and a rudimentary understanding of shift registers I would expect all leds to light up when [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] is shifted in.

So my first question is why isn't my script lighting up each led with the previous leds still lit? Let me know if you need me to clarify further! Thanks!

1

There are 1 answers

1
nekomatic On

I modified your code to print value followed by the sequence of bits[i] values you generate, and got:

1
1000000000000000
3
1100000000000000
7
1110000000000000
15
1111000000000000
31
1111100000000000
63
1111110000000000
127
1111111000000000
255
1111111100000000
511
1111111110000000
1023
1111111111000000
2047
1111111111100000
4095
1111111111110000
8191
1111111111111000
16383
1111111111111100
32767
1111111111111110
65535
1111111111111111

So you're generating the right bit patterns, but you're not clocking the data into the shift register correctly.

Assuming that data, clock and latch are actually driving the output pins you think they are, then this is an electronics question rather than a MicroPython question. But I would start by adding a time delay after you take each pin high or low.