philbegg28
Posts: 3
Joined: Fri Jun 14, 2019 10:10 pm

Using GPIO

Fri Jun 14, 2019 10:19 pm

Hi Guys,

This may seem like a stupid question but I really cant get my head around it.

I have two seperate python codes

motor.py:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT)
print "LED on"
GPIO.output(26,GPIO.HIGH)
time.sleep(5)
print "LED off"
GPIO.output(26,GPIO.LOW)
GPIO.cleanup()

motor2.py

Code: Select all

from gpiozero import LED
from time import sleep

led = LED(26)

while True:
    led.on()
    sleep(3)
    led.off()
    sleep(3)
When I run motor.py I can measure the voltage changing from 0 to 3.3v between GPIO26 and GND. If I measure the resistance between GPIO26 and gorund its an Open circuit.

When I run motor2.py I can measure the voltage changing from 0 to 3.3v between GPIO26 and GND. If I measure the resistance between GPIO26 and gorund its a 35ohms when the GPIO is High.

Now the reason I ask is Im trying to drive a L298N motor driver. motor.py does not work to drive the motor. motor2.py does work to drive the motor.

Why does motor2.py create some continuity while motor.py does not???
Last edited by philbegg28 on Sat Jun 15, 2019 12:14 pm, edited 1 time in total.

dl324
Posts: 113
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: Using GPIO

Sat Jun 15, 2019 3:17 am

You're using different GPIO's. The first script is using Broadcom numbers and the second uses Pi numbering. Comment out the setmode command in the first script and they'll use the same GPIO.

You shouldn't be making resistance measurements with a DVM on a powered circuit.

klricks
Posts: 6375
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Using GPIO

Sat Jun 15, 2019 3:32 am

You cannot measure resistance in any powered circuit with an Ohm meter.
Measuring resistance of a GPIO (powered or not) will not give any meaningful information.
When posting code on the forum you should use the code tags. (Select your code and then press the [</>] button).
Indents in Python are important......
You can edit your original post and do that.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

User avatar
rpdom
Posts: 14155
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Using GPIO

Sat Jun 15, 2019 5:39 am

dl324 wrote:
Sat Jun 15, 2019 3:17 am
You're using different GPIO's. The first script is using Broadcom numbers and the second uses Pi numbering. Comment out the setmode command in the first script and they'll use the same GPIO.
The second script is using gpiozero. That uses BCM numbering. So both scripts are using the same numbering system. No changes should be necessary.
Signature is on holiday.

B.Goode
Posts: 7754
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Using GPIO

Sat Jun 15, 2019 9:25 am

dl324 wrote:
Sat Jun 15, 2019 3:17 am
You're using different GPIO's. The first script is using Broadcom numbers and the second uses Pi numbering. Comment out the setmode command in the first script and they'll use the same GPIO.

You shouldn't be making resistance measurements with a DVM on a powered circuit.

If you comment out the GPIO.setmode() statement you will simply get an error when you try to run the script: a call to GPIO.setmode() is mandatory when using the RPi.GPIO library, and there is no default (assumed) setting.

And as already stated, the equivalent of GPIO.setmode(GPIO.BCM) is the default mode of operation for the gpiozero library.

User avatar
Burngate
Posts: 5870
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Using GPIO

Sat Jun 15, 2019 10:13 am

Im trying to drive a L298N motor driver.
motor.py does not work to drive the motor.
motor2.py does work to drive the motor.
motor.py:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT)
print "LED on"
GPIO.output(26,GPIO.HIGH)
time.sleep(5)
print "LED off"
GPIO.output(26,GPIO.LOW)
GPIO.cleanup()
motor2.py

Code: Select all

from gpiozero import LED
from time import sleep

led = LED(26)

while True:
    led.on()
    sleep(3)
    led.off()
    sleep(3)
When I run motor.py I can measure the voltage changing from 0 to 3.3v between GPIO26 and GND.
When I run motor2.py I can measure the voltage changing from 0 to 3.3v between GPIO26 and GND.
One obvious difference is that the second repeats, whereas the first only goes output high once, then immediately returns the GPIO to high-impedance input (that's what GPIO.cleanup() does)
Depending on how you've wired your L298N, it may require output low to drive the motor
When I run motor.py If I measure the resistance between GPIO26 and gorund its an Open circuit.
When I run motor2.py If I measure the resistance between GPIO26 and gorund its a 35ohms when the GPIO is High.

Why does motor2.py create some continuity while motor.py does not???
An ohmeter applies a voltage and senses the current, assuming there's no other source of power. That's meaningless since the Pi provides its own power to GPIOs set as output.
As an input, a GPIO is essentially an open circuit, and could be damaged by whatever voltage the ohmeter applies.

philbegg28
Posts: 3
Joined: Fri Jun 14, 2019 10:10 pm

Re: Using GPIO

Sat Jun 15, 2019 12:18 pm

I appreciate the feedback everyone.

Point taken from reading resistance.

I know that the motor drives when the GPIO is Set to high. The voltage is 3.3v.

Both python scripts give a reading of 3.3v when set to high however only 1 script actually makes the motor drive.

WHY?!?

EDIT: will clarify with adding sleep(5) before GPIO.cleanup()

pcmanbob
Posts: 5948
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Using GPIO

Sat Jun 15, 2019 2:54 pm

To drive a motor using an L298N you need a minimum of 2 outputs ( 3 if you want speed control ) and ground

Image

So if you look at the L298N you have 2 enable pins, which will probably be jumpered high .

then you have 4 input pins IN1-4 so for motor A you would use IN1 & IN2.

Image

now you have to drive these using 2 gpio , for stop you set both gpio low or high
for direction A you would set IN1 high and IN2 low
for direction B you would set IN1 low and IN2 high.

so using your original code and adding a second gpio output you would have code like this.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(26,GPIO.OUT) # existing gpio for IN1
GPIO.setup(19,GPIO.OUT) # added gpio output for IN2

print "Turn motor direction A"
GPIO.output(26,GPIO.HIGH)
GPIO.output(19,GPIO.LOW)
time.sleep(10)

print "Stop motor"
GPIO.output(26,GPIO.LOW)
GPIO.output(19,GPIO.LOW)
time.sleep(2)


print "Turn motor direction B"
GPIO.output(26,GPIO.LOW)
GPIO.output(19,GPIO.HIGH)
time.sleep(10)

print "Stop motor"
GPIO.output(26,GPIO.LOW)
GPIO.output(19,GPIO.LOW)
time.sleep(2)

GPIO.cleanup()
with you only driving one IN pin it was probably pot luck that one code worked and the other did not, as you had one pin floating
( not connected to anything so pin could have any voltage on it so said to be floating )
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

philbegg28
Posts: 3
Joined: Fri Jun 14, 2019 10:10 pm

Re: Using GPIO

Sat Jun 15, 2019 9:25 pm

now you have to drive these using 2 gpio , for stop you set both gpio low or high
for direction A you would set IN1 high and IN2 low
for direction B you would set IN1 low and IN2 high.
So adding in IN2 Low fixed the problem. I appreciate the help.

very strange now though that when I do comment out the GPIO.LOW the motor still drives. Just lucky timing like you said maybe?

pcmanbob
Posts: 5948
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Using GPIO

Sun Jun 16, 2019 9:08 am

But if you are only commenting out the command and not actually disconnecting the cable then the L298N IN pin in question is not floating because you still have it connected to the pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”