Page 1 of 1

Shift Register PWM

Posted: Sat Nov 02, 2019 8:49 pm
by Jereamon
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!

Re: Shift Register PWM

Posted: Sun Nov 03, 2019 5:44 am
by OutoftheBOTS_
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.

Re: Shift Register PWM

Posted: Sun Nov 03, 2019 10:15 am
by chrismas9
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.

Re: Shift Register PWM

Posted: Sun Nov 03, 2019 6:16 pm
by Jereamon
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.

Re: Shift Register PWM

Posted: Tue Nov 05, 2019 1:53 pm
by nekomatic
Yes, the transistor solution would look like this:
srpwm.png (16.59 KiB) Viewed 603 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.

Re: Shift Register PWM

Posted: Tue Dec 03, 2019 2:46 pm
by nagylzs
Instead of using transistors, I suggest that you use an ULN2003 IC. That contains an array of darlington pairs, and it already includes 2K resistors in series of the inputs. It also includes flyback diodes! That is ideal for diving leds: small footprint, multiple outputs. I have been using this (e.g. shift register + darlington array) successfully to control LEDs and relays.

This is especially effective when you have many outputs (say, 32 outputs with 4 shifts registers in cascade). If you only need to drive 7 output lines (or less) then this is a less ideal solution. It is true that you need fewer I/O pins to drive the shift register IC, but you also need to add more space on the PCB for the shift register and the transistor array. I would not use it for less than 14 output lines. If you need less than 14 lines, then it is probably better to use an ESP32 instead, and use many output pins directly. Just my one cent...

Re: Shift Register PWM

Posted: Tue Dec 03, 2019 2:52 pm
by nagylzs
Another problem with shift registers is that it is slow for PWM. You can - in theory - drive the shift register's output pins from software, but then your main program will be occupied with sending data to the shift register. There is no built-in hardware PWM for this.

On the other side, by using an ESP32 with more output pins, you can take advantage of the hardware PWM, set the PWM signal independently for each line, and do something useful in your main thread at the same time. If you add the cost of increased PCB space and more complicated software, more memory and more processing power, then a single ESP32 (without shift register) may be better than an ESP8266 with a single shift register.

Sorry, I just realized that you maybe using something totally different, e.g. a pyboard. Nevertheless, the principle is the same: shift registers does not go well with micropython and PWM.

If your only goal is to control many number of switches (slowly, e.g. no PWM) then that is a very different story.