My Circuit Kills Arduinos and It's Slowly Killing Me

ebeowulf17

Joined Aug 12, 2014
3,307
Funny how people see different things in the same post!

Position data is best taken from endstops like you mentioned or some sort of rotary encoder. Even a disk with a hole in it and some sort of optosensor to read it works. You might be able to detect a blockage by measuring current draw on the motor driver (easier at the ground return of the driver). If you see the stall current, stop the motor. You'll need to characterize your motor for stall current.

Note also that feeding any microcontroller and a motor off the same power rail is going to have problems due to the electrical noise the motor puts out. Also current draw fluctuations of the motor can be problematic. I'd use a separate LDO regulator with a blocking diode for microcontroller. This to prevent sag on the motor pulling the LDO output down. Use a largish cap as a reservior on the regulator side of the diode. Getting power distribution right is a key, but often ignore, part of any project.
Agreed on all counts.

I'd definitely go with low-side current sensing on the motor driver negative line to detect end positions. The signal coming from the shunt resistor, despite being very low voltage, might still have bizarre noise of some sort, so a small cap and a couple of clamping diodes to protect the analog inputs might still be a good idea.

And yeah, isolating the power supplies as much as possible is a good idea too. The Arduino will *probably* be happy using its own regulator straight off the battery, but running the motor driver separately (either straight from the battery or through its own dedicated regulator) seems like a really good idea. Big and little caps at the input to each board (Arduino and motor controller) will probably help as well.

I think the low side current sensing could be brutally simple. This is all off the top of my head, so I could get this all wrong, but I think you can just put a 1 ohm resistor between the motor controller negative power supply input and ground, then measure voltage on the high side of that resistor with the Arduino. At 150mA, the resistor would drop 150mV. If I remember right, 5V supply and 1024 steps means roughly 5mV ADC resolution. So 150mA should be ~30LSB, which should be more than enough to work with since we're just looking for stall current as an indication of end points, not trying to get an accurate, precision measurement. A little experimentation would be needed to find reliable real world running current and stall current values, but it should be easy enough to pull off.
 

ebeowulf17

Joined Aug 12, 2014
3,307
I *think* this article should clear up the input protection question. Full disclosure, I didn't read the whole thing, but after skimming it, it all looks right.

https://www.digikey.com/en/articles/techzone/2012/apr/protecting-inputs-in-digital-electronics

This image shows what I would picture for input protection from a dubious source (like a motor):
IMG_4117.JPG
I think the pull up resistor they show is dubious for an analog input, where it would be a no-brainer on a digital input. The two external diodes, cap, and series resistance are all in line with what I'd envision.
 

ebeowulf17

Joined Aug 12, 2014
3,307
Here's an image that gives a close approximation of the current sensing idea:
IMG_4118.GIF
As per the rough calculations in my earlier post, I think you can get away without the amp, and just feed the signal from the high side of the shunt resistor into your ADC (with the protections described in the last few posts!)
 

Thread Starter

cowhock35

Joined Dec 25, 2012
48
Agreed on all counts.

I'd definitely go with low-side current sensing on the motor driver negative line to detect end positions. The signal coming from the shunt resistor, despite being very low voltage, might still have bizarre noise of some sort, so a small cap and a couple of clamping diodes to protect the analog inputs might still be a good idea.

And yeah, isolating the power supplies as much as possible is a good idea too. The Arduino will *probably* be happy using its own regulator straight off the battery, but running the motor driver separately (either straight from the battery or through its own dedicated regulator) seems like a really good idea. Big and little caps at the input to each board (Arduino and motor controller) will probably help as well.

I think the low side current sensing could be brutally simple. This is all off the top of my head, so I could get this all wrong, but I think you can just put a 1 ohm resistor between the motor controller negative power supply input and ground, then measure voltage on the high side of that resistor with the Arduino. At 150mA, the resistor would drop 150mV. If I remember right, 5V supply and 1024 steps means roughly 5mV ADC resolution. So 150mA should be ~30LSB, which should be more than enough to work with since we're just looking for stall current as an indication of end points, not trying to get an accurate, precision measurement. A little experimentation would be needed to find reliable real world running current and stall current values, but it should be easy enough to pull off.
I believe that the motor controller negative supply input is ground in itself. Would I run the shunt between ground and the negative output of the motor? Or could I possibly use the positive VCC On the motor driver?
 

Thread Starter

cowhock35

Joined Dec 25, 2012
48
Never mind I see what you are saying, it is ground but if you put a shunt between the motor negative input and ground and measure the high side It will be measuring across the shunt. A hitch in the works is that the stall current is one amp or a little more. Assuming it is one then the voltage across it would also be 1V but that would mean the resistor is dissipating a full watt of power. Wouldn’t that blow it unless I get a heavier duty power resistor?
 

dendad

Joined Feb 20, 2016
4,476
It does depend on what sensitivity you need for the motor current sense.
An amp would be a good idea so there is less volts lost across the resistor. As you just have 5V to start with, loosing a volt for measuring the current is a lot.
I have used this in some of my boards.
Using an amp lets the sense resistor be a very low value therefore introducing little loss into the circuit.
 

ebeowulf17

Joined Aug 12, 2014
3,307
Never mind I see what you are saying, it is ground but if you put a shunt between the motor negative input and ground and measure the high side It will be measuring across the shunt. A hitch in the works is that the stall current is one amp or a little more. Assuming it is one then the voltage across it would also be 1V but that would mean the resistor is dissipating a full watt of power. Wouldn’t that blow it unless I get a heavier duty power resistor?
Yep, sorry about that. Shouldn't post when I'm that sleepy. I didn't work out the power dissipation.

As dendad says, an amplifier is probably the best solution. I was just trying to get it done small, cheap, and easy.

Still, you *might* be able to get what you need, without an amp, with a little bit of a balancing act. For example, if you use a 0.15R resistor, your stall current voltage drop becomes 150mV again, with 150mW dissipation (use 1/2W resistor for wide margin, or maybe 1/4W is ok since it should only be in this state very briefly before Arduino turns motor off.) Sense voltage from running current will be immeasurably low, but you probably don't need that anyway, just stall current.

Of course, if stall current varies wildly from motor to motor, or if 150mV of wiggle on the motor controller's negative supply is too much, you'd still be better off with the amp.
 
Last edited:

Thread Starter

cowhock35

Joined Dec 25, 2012
48
Yep, sorry about that. Shouldn't post when I'm that sleepy. I didn't work out the power dissipation.

As dendad says, an amplifier is probably the best solution. I was just trying to get it done small, cheap, and easy.

Still, you *might* be able to get what you need, without an amp, with a little bit of a balancing act. For example, if you use a 0.15R resistor, your stall current voltage drop becomes 150mV again, with 150mW dissipation (use 1/2W resistor for wide margin, or maybe 1/4W is ok since it should only be in this state very briefly before Arduino turns motor off.) Sense voltage from running current will be immeasurably low, but you probably don't need that anyway, just stall current.

Of course, if stall current varies wildly from motor to motor, or if 150mV of wiggle on the motor controller's negative supply is too much, you'd still be better off with the amp.
True, an amp wouldn't be a hard solution. If I can avoid it though, the better. As ebeowulf said, it will only be in that high current state for a fraction of a second. Otherwise, it operates at 150ma, so even with 1R resistor, voltage would be 150mV and wattage would be .0225W. When it boosts to 1A, resistor has to take a full watt for a moment, but it might handle it. To be safer, get a 1/2W resistor and lower the R even more. Just a matter of testing what kind of fluctuations exist in the signal. Bench testing on a side circuit right now with 4 10R in parallel (lowest I have on hand) and it works as it should. Of course, the system worked flawlessly back when I was measuring direct motor voltage on a side circuit...
 

ebeowulf17

Joined Aug 12, 2014
3,307
Of course, the system worked flawlessly back when I was measuring direct motor voltage on a side circuit...
So, I may be misunderstanding how you had it running with direct motor voltage measurement before, but here's my take on it, with several reasons why it's less than ideal:
If I understand you correctly, you were measuring voltage at the high side of the motor, which would have read pretty close to full scale voltage (battery or power supply voltage) when running, and then dropped to some lower voltage level when the motor stalled. Is that right?

Problem 1: Motors generate back-EMF which varies with speed, load, etc. On average, when running at the right speed, the back EMF balances the system nicely. Your meter shows you a slow, average voltage reading which nicely demonstrates this. However, the EMF is powerful and erratic, and just about the worst thing you could connect to a sensitive microcontroller input, cause its little spikes will destroy anything in their path! This can all be mitigated somewhat with the input protections we discussed above, but it's still unwise.

Problem 2: This isn't the most reliable way to see the motor state. The voltage sag you see is a function of the resistance of your hook up wires and actual sag on your power supply (and maybe limitations of your motor driver as well?) Those variables are likely to vary quite a bit in various implementations of this concept, meaning the sag you see could be highly variable. A stronger power supply and/or lower resistance in the rest of your system will result in more current through the motor, more voltage drop across the motor, and less apparent sag at the high side of the motor. It's just not a reliable way to see what you need to see. Current through the motor should be far more consistent and predictable.

There's a big difference between things you can get to work some of the time as a fun project when you're hands-on with it, vs. things that will work reliably for a long time without your supervision. If I was tinkering at home, I might play with your motor voltage idea after adding the needed input protections. But if I'm designing something for production, I'd definitely go with current sensing (or limit switches, other proven technologies, etc.)
 

Thread Starter

cowhock35

Joined Dec 25, 2012
48
So, I may be misunderstanding how you had it running with direct motor voltage measurement before, but here's my take on it, with several reasons why it's less than ideal:
If I understand you correctly, you were measuring voltage at the high side of the motor, which would have read pretty close to full scale voltage (battery or power supply voltage) when running, and then dropped to some lower voltage level when the motor stalled. Is that right?

Problem 1: Motors generate back-EMF which varies with speed, load, etc. On average, when running at the right speed, the back EMF balances the system nicely. Your meter shows you a slow, average voltage reading which nicely demonstrates this. However, the EMF is powerful and erratic, and just about the worst thing you could connect to a sensitive microcontroller input, cause its little spikes will destroy anything in their path! This can all be mitigated somewhat with the input protections we discussed above, but it's still unwise.

Problem 2: This isn't the most reliable way to see the motor state. The voltage sag you see is a function of the resistance of your hook up wires and actual sag on your power supply (and maybe limitations of your motor driver as well?) Those variables are likely to vary quite a bit in various implementations of this concept, meaning the sag you see could be highly variable. A stronger power supply and/or lower resistance in the rest of your system will result in more current through the motor, more voltage drop across the motor, and less apparent sag at the high side of the motor. It's just not a reliable way to see what you need to see. Current through the motor should be far more consistent and predictable.

There's a big difference between things you can get to work some of the time as a fun project when you're hands-on with it, vs. things that will work reliably for a long time without your supervision. If I was tinkering at home, I might play with your motor voltage idea after adding the needed input protections. But if I'm designing something for production, I'd definitely go with current sensing (or limit switches, other proven technologies, etc.)
You are correct, it was measuring at high side. It was a bad idea, as proven by consensus here and my testing. It just quit working properly last night unless it was plugged into serial, the motor wasn't shutting off even when it was stalled out.

I have adjusted the circuit to show what you suggested. I have a 2.5R coming out of the motor driver GND and I am measuring across that resistor. Saves an analog pin, and vastly reduces the voltage to that poor champ of an Uno. I am getting readings of about 80 (~39mV) on the serial output, which is about right (39mV=0.156A*2.5R). These boost to about 300 (1.46A) at stall, which is a little high, (but I could sample faster and cut it before it get that high) and it seems much more reliable than the previous system.

Found some 1/2R 2w resistors on Prime that should do the trick and replace that 2.5R cluster I have currently and that should greatly limit my power consumption and the power across the resistor even at stall should be less than 1W or so.

With this system in place (assuming it is adequate?), do I still need a protection circuit in place for the Uno?
 

ebeowulf17

Joined Aug 12, 2014
3,307
You are correct, it was measuring at high side. It was a bad idea, as proven by consensus here and my testing. It just quit working properly last night unless it was plugged into serial, the motor wasn't shutting off even when it was stalled out.

I have adjusted the circuit to show what you suggested. I have a 2.5R coming out of the motor driver GND and I am measuring across that resistor. Saves an analog pin, and vastly reduces the voltage to that poor champ of an Uno. I am getting readings of about 80 (~39mV) on the serial output, which is about right (39mV=0.156A*2.5R). These boost to about 300 (1.46A) at stall, which is a little high, (but I could sample faster and cut it before it get that high) and it seems much more reliable than the previous system.

Found some 1/2R 2w resistors on Prime that should do the trick and replace that 2.5R cluster I have currently and that should greatly limit my power consumption and the power across the resistor even at stall should be less than 1W or so.

With this system in place (assuming it is adequate?), do I still need a protection circuit in place for the Uno?
I'm no expert, but my opinion is that you still want some measure of protection against transients and potential wiring faults. Microcontrollers are just too delicate on their own.
  • The ideal is something like the graphic I shared earlier, which includes clamping diodes.
  • The bare minimum is a series resistor, which limits current through the Arduino's built in protection diodes in the event of over or under voltage transients.
  • A reasonable compromise might be an RC filter (resistor between current-sense voltage wire and Arduino input, with capacitor from input pin to ground.)
There's plenty of room for debate on questions like this, so keep in mind that the notes above are just my opinion.
 

ebeowulf17

Joined Aug 12, 2014
3,307
You are correct, it was measuring at high side. It was a bad idea, as proven by consensus here and my testing. It just quit working properly last night unless it was plugged into serial, the motor wasn't shutting off even when it was stalled out.

I have adjusted the circuit to show what you suggested. I have a 2.5R coming out of the motor driver GND and I am measuring across that resistor. Saves an analog pin, and vastly reduces the voltage to that poor champ of an Uno. I am getting readings of about 80 (~39mV) on the serial output, which is about right (39mV=0.156A*2.5R). These boost to about 300 (1.46A) at stall, which is a little high, (but I could sample faster and cut it before it get that high) and it seems much more reliable than the previous system.

Found some 1/2R 2w resistors on Prime that should do the trick and replace that 2.5R cluster I have currently and that should greatly limit my power consumption and the power across the resistor even at stall should be less than 1W or so.

With this system in place (assuming it is adequate?), do I still need a protection circuit in place for the Uno?
FWIW, I think you've got errors in some of your calculations. Here's what I get for raw ADC readings of 80 and 300:
upload_2018-1-4_12-45-43.png
This doesn't change the plan at all, but I thought it was worth noting.
 

Attachments

Thread Starter

cowhock35

Joined Dec 25, 2012
48
UPDATE: After implementing the circuit ebeowulf discussed, the unit is operating flawlessly and has for a couple days so I think it's time to move to the next steps. Ultimately, I want to use the Atmega off board, and I have attached the current complete circuit as well as a couple circuits I would like to add on.
IMG_0078.jpg

First, the off board Atmega. I have used this before, it is pretty straightforward as far as I know, except I am removing any internal circuit protection that the Uno board provides. Will the power input from the LM7805 with decoupling caps and a diode be adequate protection? Will all the floating pins cause any issues?
IMG_0079.jpg

Second, the pin protection circuit. I think that I may put this on A5 an A4 which take voltage inputs that may have some variance. Is it overkill? I think it might be. With that in mind, I plan to add just a current limiting resistor on A0-3 and D10-11 because the inputs shouldn't have a high degree of variance. Is this good thinking?
IMG_0080 (1).jpg

Third, the charge controller circuit. Originally, we planned to get a cheap little panel and a big battery and trickle charge it. Unfortunately the circuit uses way more power than planned (60mA resting, part of the reason for getting the Atmega off the Uno board) and the panels we tried were completely worthless so in upping the panel quality and size we are getting more power. Using a 3AH 6V battery and a 1.8W 6V panel is the going theory. This circuit I whipped up would have a digital output pin controlling a Mosfet to allow the Solar panel + to charge the Battery +. The code would shut the charge off, an analog pin would check the battery voltage, after cutting it in half with a divider, and if it was low, the switch would turn back on for a time period, then turn off and check again until it was charged. I have limited experience with solar charging so no sure if this scheme is valid or not.

Finally, a couple more general questions:
Does it matter what kind of diodes I use? There seem to be a lot of kinds with minimal differences are far as power protection is concerned.
Would it be wise to include a replaceable or PTC fuse somewhere in the system?
Do I need any pin protection on my output pins?

Thanks so much for all the help so far!
 

ebeowulf17

Joined Aug 12, 2014
3,307
UPDATE: After implementing the circuit ebeowulf discussed, the unit is operating flawlessly and has for a couple days so I think it's time to move to the next steps. Ultimately, I want to use the Atmega off board, and I have attached the current complete circuit as well as a couple circuits I would like to add on.
View attachment 143120

First, the off board Atmega. I have used this before, it is pretty straightforward as far as I know, except I am removing any internal circuit protection that the Uno board provides. Will the power input from the LM7805 with decoupling caps and a diode be adequate protection? Will all the floating pins cause any issues?
View attachment 143121

Second, the pin protection circuit. I think that I may put this on A5 an A4 which take voltage inputs that may have some variance. Is it overkill? I think it might be. With that in mind, I plan to add just a current limiting resistor on A0-3 and D10-11 because the inputs shouldn't have a high degree of variance. Is this good thinking?
View attachment 143122

Third, the charge controller circuit. Originally, we planned to get a cheap little panel and a big battery and trickle charge it. Unfortunately the circuit uses way more power than planned (60mA resting, part of the reason for getting the Atmega off the Uno board) and the panels we tried were completely worthless so in upping the panel quality and size we are getting more power. Using a 3AH 6V battery and a 1.8W 6V panel is the going theory. This circuit I whipped up would have a digital output pin controlling a Mosfet to allow the Solar panel + to charge the Battery +. The code would shut the charge off, an analog pin would check the battery voltage, after cutting it in half with a divider, and if it was low, the switch would turn back on for a time period, then turn off and check again until it was charged. I have limited experience with solar charging so no sure if this scheme is valid or not.

Finally, a couple more general questions:
Does it matter what kind of diodes I use? There seem to be a lot of kinds with minimal differences are far as power protection is concerned.
Would it be wise to include a replaceable or PTC fuse somewhere in the system?
Do I need any pin protection on my output pins?

Thanks so much for all the help so far!
Wow, lot's of questions there! I'll do my best to help where I can:
  • I could be wrong, but I *think* the capacitor in your input protection scheme should be in between the resistor and the input pin. With the series resistor and diodes in place, you're well protected regardless, but it's worth double checking if I'm remembering that right.
  • I don't know enough about regulators and such to comment on your power supply scheme, but suspect it's fine.
  • Unused GPIO pins on the Arduino should ideally be set as inputs and held at a fixed voltage, either by using external pull up resistors, or by enabling the built in pull up resistors in code. Link: https://forum.arduino.cc/index.php?topic=57067.0
  • I think the input protection for your motor sensing inputs is a good idea. Regarding other input pins (A0-3 and D10-11,) it depends on layout. If those signals come through wires off the board, protection is a good idea. If they come from components that are on the same PCB as the controller itself, and you don't run their traces too close to noisy, high current traces, they may not need any protection at all.
  • I know nothing about battery charging systems, except that lithium batteries are notoriously tricky. If you don't already know what you're doing with them, you probably shouldn't try. If it's a lead acid battery, much easier to home brew a solution, but still out of my area of knowledge. This is potentially a big subject and might be worth its own separate thread if you don't get answers within this one.
  • Not sure about output pin protection. Do outputs leave the board, or just send signals within the board?
 

Thread Starter

cowhock35

Joined Dec 25, 2012
48
You are correct, I corrected the cap location.

Looks like I should name all the unused pins as inputs and then leave them unconnected to anything. Thanks for the link.

Most of the signals for A0-A3 and D10-11 will come from the board. One of them will be on 6ish inch wires to an LDR but the rest will be all on the PCB. And an LDR is nothing but a resistor, so there seems little point in adding a resistor in line with another resistor. Perhaps I don't need protection on them at all. Just worried about users "grubby fingering" the board and chip and frying things by shorting things out. It will be designed with a replaceable chip so if we have to update firmware or the chip fries it can be replaced for a couple bucks instead of the whole board.

Agreed, the charging system is probably topic for another forum. I will do some more research, but possibly will just scrap the idea and get a 4 dollar PWM controller and splice it in.

The output pins will just go to a physical 3 position switch on the board.
 

philba

Joined Aug 17, 2017
959
Wow, lot's of questions there! I'll do my best to help where I can:
  • I could be wrong, but I *think* the capacitor in your input protection scheme should be in between the resistor and the input pin. With the series resistor and diodes in place, you're well protected regardless, but it's worth double checking if I'm remembering that right./QUOTE]
If the cap and resistor form a low pass filter then that's the correct arrangement. You need to be careful about the cut off frequency, though as that can affect the pulse shape.
 

ebeowulf17

Joined Aug 12, 2014
3,307
If the cap and resistor form a low pass filter then that's the correct arrangement. You need to be careful about the cut off frequency, though as that can affect the pulse shape.
Agreed, a good thing to keep in mind, depending on the nature of the signal. I think in this case, monitoring DC current to watch for stall current, it shouldn't really be an issue unless you get into incredibly high RC values and time constants.
 
Top