L298N + Raspberry Pi + NPN = Rabbits

Thread Starter

sirchuck

Joined Feb 14, 2016
131
The part in question:
https://www.amazon.com/TIMESETL-L29...n&qid=1552463514&s=gateway&sr=8-2-spons&psc=1

I'm planning on controling the power to the L298N via the Pi using an NPN transistor. With an LED this is very easy:

Source -> a resistor -> load -> Collector -> Emitter -> Common Ground
..................................................................|
..................................................................> Pi Source -> Base - > Emitter -> Common Ground

But the stupid L298N (which is awesome not stupid) has screw downs that have 1 common ground the Pi and the L298N both use. This means it messes up the way every internet teacher explains the NPN route.

Source -> Resistor -> L298N Positive -> L298N Common Ground with Pi on same screw down -> Collector -> Emitter -> Ground again???

So, in a situation like this can I put the NPN before connecting the positive wire to the L298N, or do I really attach the NPN after the circuit comes back out of the L298N?

It's really hard for me to explain this to you properly, so if you need me to elaborate on any part let me know.

Thanks!
 

pmd34

Joined Feb 22, 2014
518
Hi sirchuck, the simple answer is; No, you cant [shouldn't] do it!
Actually the main problem is that even if you disconnect the ground to the PCB, if you have the positive supply connected, and the digital data lines, then it may try to power up parts of circuit using these which could damage those digital lines. If you really have to switch the power to the board then you should switch the positive supply line.

Its not clear why you need to however as the "enable" lines on the L298 allow you to completely shut down all power to the motor.
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
Its not clear why you need to however as the "enable" lines on the L298 allow you to completely shut down all power to the motor.
Well, maybe I don't have to. The reason I even thought I may need it is when my Raspberry Pi gets shut off when there is a cloud passing over for example, and then starts back up, the GPIO pins might trigger randomly. At least that's what I read a few times, or saw in some videos.

My thought was, just stop all power to the L298 until I want to actually control a motor, then I'd activate the transistor, allow power, and then control the motors.

https://www.raspberrypi.org/forums/viewtopic.php?t=47895
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
Oh ok, I see how to do it now. The simple things are still elusive to me.

Basically, I just need to cut the positive wire. Duh. What a stupid mistake. I'm just looking at the circuit and I fooled myself into thinking if I put the transistor on the positive wire it would make the bit of wire between the transistor and the L298 turn negative and it didn't make sense to put a negative wire into the + post of the L298. But that's not how that works. :D

All good again, thanks!
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
Hehe no worries, its how we all learn.. though often with a few poofs of smoke in the beginning!
Wait, actually I am confused again based on how I always see NPN circuits built. Hmm, maybe I actually need to us a PNP?

Here is the problem usually you put a resistor followed by the load followed by the NPN. If I just cut my wire and throw the NPN in above the load that's not going to work right? Or can I put the NPN above the L298 and it will still work fine?

It's just the circuits I've seen always put the load before the NPN, but that would be impossible the way the L298 works.

Is it ok to just put an NPN above your load with no resistor? The L298 has resistors built in so it doesn't really need more.
 

pmd34

Joined Feb 22, 2014
518
You could use a PNP on the positive supply I guess yes.
You really want that NPN dont you!
I suppose if you really really must have it then, the best place would be in place of RS1 & RS2 in this schematic (you could just connect pin 1 &15 together and just use one NPN.)
Capture2.PNG
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
Ahh, ok so I wasn't wrong initially, I actually was wrong about being able to just cut the positive wire and throwing an NPN there. I don't think I'm ready to play with actually working directly on the board, and there is no reason to use a PNP if I understand it, it needs positive voltage on the base before it shuts off power to the L298, which make it pointless for what I'm trying to prevent.

By the time I tell the GPIO to provide power to the PNP base, the car will be driving all over the place. :D

So yea, I'll scrap the idea and just let it drive around wildly, it shouldn't get far before the GPIO pins are set up.
 

dendad

Joined Feb 20, 2016
3,666
Often, in industrial controls, a"watchdog" type of circuit is used to enable things like motor drives. What you can do is to have a Pi output pin toggling at maybe 1kHz. Then, capacitivly couple this to a rectifier and have the rectifier output feed to a Schmitt trigger circuit that is used to drive the enable pins of the L298.
This is better that having just a straight port pin enabling the chip because the Pi has to keep running to generate the pulses. If just a port is used, the Pi could crash with the port pin held in the enable state.

Now, the Pi has to start up, generate the pulses, (not with any built in timers or PWM functions), but an invert pin inside the program loop, before the output can operate.
If the Pi hangs, after a short delay, the output will turn off. A very comforting idea when the device may be driving big heavy machinery!

Awake.jpg

Here is the "awake" part of one board. The "AWAKE" signal is generated by the processor and rectified to drive a Schmitt trigger, the output then used to drive an opto coupler.
This opto could be connected between the enables and gnd, with a pullup resistor to the +5V supply.

You don't have to use an opto, but they are handy for level shifting and isolation. On this board that has been well used many places in industrial control service for the last 20 years, the opto is used to both level shift and isolate the PIC controller from the 24V I/O circuitry. But you could just use a FET or NPN transistor in place of the opto.
 
Last edited:

Thread Starter

sirchuck

Joined Feb 14, 2016
131
I feel like that was a very nice post that many other people should probably read, but I lost you at Often. I guess I'll bookmark this page in case I ever get educated on electronics and then have a re-read.

All this should have been avoided anyway, initially, all I wanted to do was have the Pi allow my Solar Panel to charge the batteries, and then disable the charge while the Pi runs the battery-powered motor controller. Once motor operations were finished with software, it would switch back on a GPIO pin to tell a transistor to re-allow the solar panel to charge the batteries.

But I can't even do that because I still have the same problem with the load being below the transistor. So you can't have:
Solar Panel -> (Resistor?) -> Battery -> collector -> emitter
Instead, you have to go
Solar Panel -> collector -> emitter -> (Resistor?) -> Battery

Which you apparently can not do with an NPN.

Plus apparently, I needed to buy a special buck converter to properly charge a battery called an MPPT.
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
Hmmm, wait you kept using the word opto and I had to look it up but it wasn't helpful. Then I discovered this:
https://uk.farnell.com/clare/lca710...mOd484aKvSoBytacDqycI3UUyttDjhLhoChY8QAvD_BwE

Apparently, it's a solid state relay :D That sounds like a great solution!

I'll have to do some research to see if this means I can use the Pi's low GPIO power to close the circuit for the higher voltage Solar Panel, but if it works the way i think, that sounds great, thanks!
 

dendad

Joined Feb 20, 2016
3,666
Try to draw a circuit and post a picture of what you want. I'm having trouble figuring out what you need.

Here is a circuit that can switch power to a load but have the switch in the +ve line.
HighSideSwitch.png
And a MPPT charge controller is a good way to look after the battery. It will protect from over charging and discharging. Usually, they are for lead acid batteries.

By the time I tell the GPIO to provide power to the PNP base, the car will be driving all over the place. :D
So yea, I'll scrap the idea and just let it drive around wildly, it shouldn't get far before the GPIO pins are set up.
The watchdog circuit above will prevent this. When the power is applied, the enable pins on the motor driver will be high as they will be pulled up to 5V by a couple of 10K resistors you add to the circuit. As these pins need to be low (0V) to turn on the motor driver, the motors will not run.
Then, the Pi starts up and run the program. One thing the program does is to pulse an output that generates a voltage that rises and turns the Schmitt trigger inverter on. As it is an inverter, the output that was at +5V now goes to 0V and enables the motor driver.
And if the Pi stops running, the volts on the inverter input falls so the output goes to 5V again and turns off the motors safely.
 

dendad

Joined Feb 20, 2016
3,666
Apparently, it's a solid state relay :D That sounds like a great solution!
A Solid State Relay (SSR) usually is a "opto" but what I an referring to is a lower powered version. An internal LED shines onto a photo transistor and so turns it on. There are a range of "opto isolators" or "opto couplers" available.
If you buy a SSR to do the switching, make sure you get one designed for DC as a lot of them are for AC only as they have a Triac in then and the opto signal will turn them on but it needs the zero current of an AC cycle to turn off again.
http://www.omron.com.au/service_support/FAQ/FAQ02101/index.asp

For instance, I use a lot of these...
http://www.sharp-world.com/products/device/lineup/data/pdf/datasheet/pc357n_e.pdf
They are good for switching small signals, not a lot of power.

That Farnell link shows one good for 1Amp only.

These are a LOT bigger :)
https://www.jaycar.com.au/solid-state-relay-4-32vdc-input-30vdc-100a-switching/p/SY4086
 

Thread Starter

sirchuck

Joined Feb 14, 2016
131
A Solid State Relay (SSR) usually is a "opto" but what I an referring to is a lower powered version. An internal LED shines onto a photo transistor and so turns it on. There are a range of "opto isolators" or "opto couplers" available.
If you buy a SSR to do the switching, make sure you get one designed for DC as a lot of them are for AC only as they have a Triac in then and the opto signal will turn them on but it needs the zero current of an AC cycle to turn off again.
http://www.omron.com.au/service_support/FAQ/FAQ02101/index.asp

For instance, I use a lot of these...
http://www.sharp-world.com/products/device/lineup/data/pdf/datasheet/pc357n_e.pdf
They are good for switching small signals, not a lot of power.

That Farnell link shows one good for 1Amp only.

These are a LOT bigger :)
https://www.jaycar.com.au/solid-state-relay-4-32vdc-input-30vdc-100a-switching/p/SY4086
Your first post was beyond me, I'd have to research the different words before I even begin to attempt that. Watchdog Circuit / Enable PIn / Schmitt trigger inverter, I caught none of that.

I bought some basic relays, the plan now is just to cut power to the Motor Driver (L298N) with the Normally Open portion, and then when I want to give power to the L298N I'll just let the Raspberry trigger the relay. :)

I do have a few questions derivative of what you said though:
1) MPPT - I'm using a Li-ion battery 3.7v 400mAh, not lead acid, does that matter?
2) MPPT - While people use MPPT's to charge batteries, there's no problem running my other electronics like my Pi off an MPPT right? It's just overkill and expensive for that purpose?
3) I want to buy some of the SSR's you linked to play with, what are they called so I can type it in Ebay? I want the ones you listed first:
http://www.sharp-world.com/products/device/lineup/data/pdf/datasheet/pc357n_e.pdf

Or is it just type SSR and dig until I find something DC-DC? I'd really like a few of the ones you linked though, looks really nice and easy to work with.
 

dendad

Joined Feb 20, 2016
3,666
Those SSRs look to be ok.
Have a look at the L298 data sheet.
On the L298 device, there are a couple of pins that drive the outputs, "IN1" and "IN2".
Also, there is a couple ENABLE pins, "ENABLE A" and "ENABLE B". These pins must be at 0V to turn the device on, that is, to Enable it.
You may find there is no need to switch the power to the L298 on and off, just switch the ENABLE pins to 5V and the thing will draw a lot less power.

A "WatchDog" is a circuit that resets the controller if the program crashes, like a watch dog will call attention to your house if something is amiss. If you are driving motors, and it is important to not let them run wild, use of a separate circuit to disable the motors in the event of your controller going away is a good idea. In this case, the L298 can only be enabled by the watchdog circuit. And you can think of that as like a timer that the controller has to reset to prevent it timing out and turning the motor off. That is a very good idea to implement so you do not loose control for any length of time.

A Schmitt Trigger is a switch that operates at a set voltage level, not just slowly starting to turn on over an increasing range of volts, like a transistor or FET will do, but rapidly switches as the threshold voltage is reached. This is very handy for producing a fast defined switch function from a slow changing signal. In this case, it detects the charging of the watchdog timing capacitor and produces the ENABLE signals when the rectified pulses from the Pi keep going. If they stop, either HI or LO, the watchdog will turn the motors off after a short delay.
A bit of Googling will help.
 
Top