amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Using LM358 Op Amp to step up clock output from gpio

Sun Aug 09, 2020 10:21 am

Hello everyone,

I want to use Raspberry Pi 4B to control a stepper motor, but its driver is not reading clock input. - https://www.anaheimautomation.com/produ ... =86&cID=20
So I believe the clock voltage needs to be over 3.5V.
For clock, I am running a python script and connected the output to the op-amp circuit.
For some reason, the Op-Amp circuit works as expected(LED blinks) when it is powered by 5V and GND of RPi.
But when op-amp is powered with an external source it doesn't work (LED is always on). The DMM shows ground pin of RPi and Ground of power source is at a potential difference of 8V when the clock is connected but at 0.01V when the clock is not connected to the circuit. :shock:

Any help is appreciated & thanks in advance!

Code: Select all

 while True:
     gpio.output(7, True)
     time.sleep(0.1)
     gpio.ouput(7, False)
     time.sleep(0.1)
     
Attachments
OP AMP circuit 2.PNG
This Works
OP AMP circuit 2.PNG (16.21 KiB) Viewed 624 times
OP AMP circuit.PNG
This does not work
OP AMP circuit.PNG (27.74 KiB) Viewed 624 times

LTolledo
Posts: 3744
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Using LM358 Op Amp to step up clock output from gpio

Sun Aug 09, 2020 2:44 pm

did you connect the ground wires of the RPi and the OP Amp? (Y/n)?
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

jayben
Posts: 76
Joined: Mon Aug 19, 2019 9:56 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Mon Aug 10, 2020 8:35 am

An op-amp amplifies the difference between the signals at its '+' and '-' terminals. Furthermore, many op-amps will only amplify signals if they are significantly above ground potential, and below the supply potential (unless it has a 'rail-to-rail' capability).

So if you tie the the negative pin to ground, and the positive signal is also at ground, not a lot will happen; the device might try to amplify the miniscule difference between the two pins, which will depend on your wiring.

Your 24V circuit is slightly better, since the '-' pin is no longer at ground, so it is (just about) possible to detect a 0V signal, but hanging the supply pin off a 10K potential divider is asking for trouble; you could easily have a situation where, as the op amp changes from an 'off' to 'on' state, it draws more current from the supply, which briefly reduces the supply voltage and causes oscillation. These devices are designed to work with stable regulated supplies.

The stepper data sheet says "The inputs are capable of running from either open collector or TTL level logic outputs", so why not do that? just connect your logic output to the base of an NPN transistor via a resistor (say 1K), ground the emitter, and connect the collector to the stepper input. Alternatively use a 5V level shifter (such as on a Sparkfun board), since a 5V signal should be sufficient.

amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Re: Using LM358 Op Amp to step up clock output from gpio

Wed Aug 12, 2020 5:57 am

LTolledo wrote:
Sun Aug 09, 2020 2:44 pm
did you connect the ground wires of the RPi and the OP Amp? (Y/n)?
No, I haven't.

stevend
Posts: 276
Joined: Fri Oct 11, 2013 12:28 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Wed Aug 12, 2020 12:36 pm

Also, in the second diagram R1 is too high a value, and if the 24V supply is of reasonable quality, could be replaced by a link.
The LM358 is going to take some current all the time, which is going to reduce its supply voltage due to voltage drop across R1.
Then when the LED turns on that will take more current, which will reduce the supply voltage to the LM358, which may be sufficient to turn off the LED, so the LM358's supply voltage rises.....

It's sensible to add power supply decoupling to any active device; in this case something like 100nF ceramic capacitor across the supply pins of the LM358.

Any circuit implementing a comparator needs a reasonable quality (noise-free) supply; a quick fix to remove the worst of it would be to make R1 a low value (probably 10-100 ohms) rather than zero.

A further 100nF capacitor across R3 would ensure the switching threshold is stable in the presence of supply or input noise.

Finally, you could add hysteresis to be absolutely certain that the output doesn't oscillate on the switching edges. Connect the GPIO output to the '+' input via a 10K resistor. Connect a 1M resistor between '+' and output of the LM358. This ensures that, once the output has switched, a significant input voltage change is required before the output will switch back again.

Incidentally, I'm assuming that your stepper card needs a 24 volt clock. If that's incorrect, replace R1 with a voltage regulator which generates the correct output voltage; a 7805 or 78L05 would probably be OK for a 5V clock. Or you can use a potential divider on the output (two resistors) to generate the correct voltage; but their values will also be afffected by any input resistance on the stepper card. But for this I'd suggest that you use a different approach - something like a 74HCT14 or a single-gate equivalent which can be powered from 5V, and has an input switching voltage threshold of around 1 volt.

Edit: Had a better look at the stepper card data; if JP1 is in position 1-2 a 0-5V drive to the inputs would be fine, so you could use 74HCT14. (And don't forget to connect that ground/0V wire!)

amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Re: Using LM358 Op Amp to step up clock output from gpio

Thu Aug 13, 2020 1:37 am

jayben wrote:
Mon Aug 10, 2020 8:35 am

The stepper data sheet says "The inputs are capable of running from either open collector or TTL level logic outputs", so why not do that? just connect your logic output to the base of an NPN transistor via a resistor (say 1K), ground the emitter, and connect the collector to the stepper input. Alternatively use a 5V level shifter (such as on a Sparkfun board), since a 5V signal should be sufficient.

Thank you for your reply. Unfortunately, I do not know how to use transistors. Can you please refer me to a reference where I can read about it or give me a wiring diagram using 2N2222 and resistors. I have some 2N2222 a331 transistors and resistors from 10 Ohms to 1M Ohms. Based on my understanding of driver datasheet, I need to use transistors as switches which satisfy the following requirements: ground when closed & high impedance when the switch is open.

Thanks again for your help.
transistor requirements.PNG
transistor requirements.PNG (25.32 KiB) Viewed 449 times

jayben
Posts: 76
Joined: Mon Aug 19, 2019 9:56 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Thu Aug 13, 2020 9:02 am

Here you are:
inverter.png
inverter.png (7.76 KiB) Viewed 429 times
When the input is high, the circuit puts around 3 mA into the base; this makes the transistor conduct, so the collector approaches the emitter potential, i.e. it is like a closed switch between emitter and collector. When the input is low, the transistor stops conducting, and your load will pull the collector high, so it is like an off switch.

amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Re: Using LM358 Op Amp to step up clock output from gpio

Fri Aug 14, 2020 9:18 pm

jayben wrote:
Thu Aug 13, 2020 9:02 am
Here you are:

inverter.png

When the input is high, the circuit puts around 3 mA into the base; this makes the transistor conduct, so the collector approaches the emitter potential, i.e. it is like a closed switch between emitter and collector. When the input is low, the transistor stops conducting, and your load will pull the collector high, so it is like an off switch.
Thank you for your response jayben, I assembled it but now the motor stops on both 0V and 3.3V and works when I disconnect gpio pin from the breadboard.
the multimeter shows ground of power supply and GND of RPi is varying.
some additional information to help in troubleshooting:

when GPIO is disconnected from base:
collector-emitter voltage = 5V
collector base voltage = 4.08V
RPi GND and power supply GND = 3.3V (RPi GND higher voltage)

when GPIO GND is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.65V
voltage across 1K resistor = 0.42V
RPi GND and power supply GND = 1.1V (RPi GND higher voltage)

when GPIO 3.3V is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.69V (base is at higher Voltage)
voltage across 1K resistor = 1.02V
RPi GND and power supply GND = -1.56V (Power supply GND is at higher Voltage)

there is a photo of my assembly and intended setup is attached in case I connected anything wrong

thanks again!
Attachments
intended setup.PNG
Intended setup
intended setup.PNG (9.3 KiB) Viewed 362 times
setup.jpeg
black wire = Power supply GND
red wire = motor driver output
olivegreen wire = RPi GPIO pin
setup.jpeg (136.06 KiB) Viewed 362 times

drgeoff
Posts: 10916
Joined: Wed Jan 25, 2012 6:39 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Sat Aug 15, 2020 8:40 am

amarjiitpandde wrote:
Fri Aug 14, 2020 9:18 pm
jayben wrote:
Thu Aug 13, 2020 9:02 am
Here you are:

inverter.png

When the input is high, the circuit puts around 3 mA into the base; this makes the transistor conduct, so the collector approaches the emitter potential, i.e. it is like a closed switch between emitter and collector. When the input is low, the transistor stops conducting, and your load will pull the collector high, so it is like an off switch.
Thank you for your response jayben, I assembled it but now the motor stops on both 0V and 3.3V and works when I disconnect gpio pin from the breadboard.
the multimeter shows ground of power supply and GND of RPi is varying.
some additional information to help in troubleshooting:

when GPIO is disconnected from base:
collector-emitter voltage = 5V
collector base voltage = 4.08V
RPi GND and power supply GND = 3.3V (RPi GND higher voltage)

when GPIO GND is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.65V
voltage across 1K resistor = 0.42V
RPi GND and power supply GND = 1.1V (RPi GND higher voltage)

when GPIO 3.3V is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.69V (base is at higher Voltage)
voltage across 1K resistor = 1.02V
RPi GND and power supply GND = -1.56V (Power supply GND is at higher Voltage)

there is a photo of my assembly and intended setup is attached in case I connected anything wrong

thanks again!
RPi GND and power supply GND should be connected together and therefore always at the same voltage.
Quis custodiet ipsos custodes?

jayben
Posts: 76
Joined: Mon Aug 19, 2019 9:56 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Sat Aug 15, 2020 9:13 am

Absolutely right; the grounds of the Pi, the transistor, and the stepping signals must all be tied together. If you can measure any voltage between them, there is something wrong.

amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Re: Using LM358 Op Amp to step up clock output from gpio

Sat Aug 15, 2020 11:28 pm

drgeoff wrote:
Sat Aug 15, 2020 8:40 am
amarjiitpandde wrote:
Fri Aug 14, 2020 9:18 pm
jayben wrote:
Thu Aug 13, 2020 9:02 am
Here you are:

inverter.png

When the input is high, the circuit puts around 3 mA into the base; this makes the transistor conduct, so the collector approaches the emitter potential, i.e. it is like a closed switch between emitter and collector. When the input is low, the transistor stops conducting, and your load will pull the collector high, so it is like an off switch.
Thank you for your response jayben, I assembled it but now the motor stops on both 0V and 3.3V and works when I disconnect gpio pin from the breadboard.
the multimeter shows ground of power supply and GND of RPi is varying.
some additional information to help in troubleshooting:

when GPIO is disconnected from base:
collector-emitter voltage = 5V
collector base voltage = 4.08V
RPi GND and power supply GND = 3.3V (RPi GND higher voltage)

when GPIO GND is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.65V
voltage across 1K resistor = 0.42V
RPi GND and power supply GND = 1.1V (RPi GND higher voltage)

when GPIO 3.3V is connected to base:
collector-emitter voltage = 0V
collector-base voltage = -0.69V (base is at higher Voltage)
voltage across 1K resistor = 1.02V
RPi GND and power supply GND = -1.56V (Power supply GND is at higher Voltage)

there is a photo of my assembly and intended setup is attached in case I connected anything wrong

thanks again!
RPi GND and power supply GND should be connected together and therefore always at the same voltage.
Thank you very much for your response @jayben & drgeoff. So, I can connect RPi GND pin with power supply GND with a jumper cable or are there any precautions I need to make sure my RPi doesn't burn due to excess current or something else?

LTolledo
Posts: 3744
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Using LM358 Op Amp to step up clock output from gpio

Sat Aug 15, 2020 11:35 pm

if you're that "too cautious" why not place a 0.25A fuse between the two GNDs
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

jayben
Posts: 76
Joined: Mon Aug 19, 2019 9:56 pm

Re: Using LM358 Op Amp to step up clock output from gpio

Sun Aug 16, 2020 9:29 am

Modern power supplies generally have their +ve and -ve outputs 'floating' with respect to earth, i.e. there is no connection between these pins and ground, so you can connect them anywhere you like. If in doubt, use your DMM on the resistance setting to measure between the -ve (or 0V) output, and the earth pin of its mains plug - if this is a high value (or it has no earth pin) then the supply is floating, and no current can flow to earth through it.

Don't forget, in order to damage something, there has to be a complete circuit with significant current flow. If at least 1 of your supplies is floating, and the only connection between them is a single wire linking their -ve (or 0V) terminals, then there is no circuit, and no current can flow - use your DMM to check for any current in that wire, if you are still worried.

amarjiitpandde
Posts: 6
Joined: Sun Aug 09, 2020 9:48 am

Re: Using LM358 Op Amp to step up clock output from gpio

Mon Aug 31, 2020 1:21 am

jayben wrote:
Sun Aug 16, 2020 9:29 am
Modern power supplies generally have their +ve and -ve outputs 'floating' with respect to earth, i.e. there is no connection between these pins and ground, so you can connect them anywhere you like. If in doubt, use your DMM on the resistance setting to measure between the -ve (or 0V) output, and the earth pin of its mains plug - if this is a high value (or it has no earth pin) then the supply is floating, and no current can flow to earth through it.

Don't forget, in order to damage something, there has to be a complete circuit with significant current flow. If at least 1 of your supplies is floating, and the only connection between them is a single wire linking their -ve (or 0V) terminals, then there is no circuit, and no current can flow - use your DMM to check for any current in that wire, if you are still worried.
No current flowing through the power supply GND & RPi GND pin.
Thank you very much for your help.

Return to “Automation, sensing and robotics”