Shift Register PWM

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 2
Joined: Sat Nov 02, 2019 8:39 pm

Shift Register PWM

Post by Jereamon » Sat Nov 02, 2019 8:49 pm

Hello, I'm guessing this or something like it has been covered before, but I'm struggling to get this working.

I have a shift register working with a nodemcu controlling 8 leds using what I believe would be referred to as bit banging in my code.
Something along the lines of:

Code: Select all

for i in byte:
With the `byte` being something like:

Code: Select all

byte = [1, 0, 0, 1, 0, 0, 0, 1]
I'm hoping to be able to dim the leds and am wondering if anyone has any recommendations for implementing pwm over a shift register. I'm on my phone at the moment but can post additional code examples later if need be.

Thanks for your help!

Posts: 666
Joined: Mon Nov 20, 2017 10:18 am

Re: Shift Register PWM

Post by OutoftheBOTS_ » Sun Nov 03, 2019 5:44 am

First of all this can be all done much easier using addressable LEDs like Neopixels (shift register is pretty old school)

If you did want to implement it I think you will need to build a little circuitry. The shift register would operate a transitor that turned on the power supply to each LED then at the start of the power supply it could be dimmed by another transistor controlled by PWM from another pin.

Posts: 123
Joined: Wed Jun 25, 2014 10:07 am

Re: Shift Register PWM

Post by chrismas9 » Sun Nov 03, 2019 10:15 am

If the supply voltage is less than the reverse breakdown voltage of the LEDs you may not need individual transistors on the shift register outputs, just one transistor or MOSFET on the common GND side of the LEDs driven by a PWM signal.

If you only want say 16 levels of brightness you may be able to do it in software with your existing circuit.

Create 1mS timer. In the callback increment a 4bit counter with rollover. For each LED test if the required brightness value is greater than the counter and set the corresponding bit in the byte. Then shift the data out. The LEDs will be refreshed at above 50Hz and will not flicker.

Posts: 2
Joined: Sat Nov 02, 2019 8:39 pm

Re: Shift Register PWM

Post by Jereamon » Sun Nov 03, 2019 6:16 pm

Thanks for the replies!
Not sure I completely understand the transistor solution. Would that mean each led could be turned on or off by the shift register, but if one is on it would be at the same brightness as all others that are turned on?

I'll probably end up working with neopixels since it sounds like they make this problem a non-issue. But I'm new to electronics and was "having fun" trying to sort out problems that have already been solved by smarter people. Haha.

I did start writing code to try to vary the brightness using bit modulation and had it working, but only in a very limited capacity.

Posts: 18
Joined: Thu May 08, 2014 9:31 pm

Re: Shift Register PWM

Post by nekomatic » Tue Nov 05, 2019 1:53 pm

Yes, the transistor solution would look like this:
srpwm.png (16.59 KiB) Viewed 227 times
Apologies for the horrible diagram, I was trying out the Digi-key online schematic editor... would not buy again ;) The blue thing is supposed to be your MicroPython board and the pink one is the shift register. The bottom wire, from the transistor emitter, is supposed to be ground.

You could use an NPN bipolar transistor as shown, with the base resistor chosen to give adequate current, or an N-channel MOSFET if it has a low enough threshold voltage, with say 100 ohms between the driving pin and the gate so as to limit current spikes due to the gate capacitance.

Post Reply