PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Inverting a GPIO pin

Tue Oct 28, 2014 6:57 am

Greetings all. Rpi newby with a couple of Qs:

1) I'm trying to make a GPIO output (p1 pin 12) the opposite of another (p1 pin 11). First I tried GPIO.output(12, not 11) but no joy so then I tried GPIO.output(12, not GPIO.input(11)) & success. How come I have to read pin 11 to invert it? Why can't I just invert it's output register? I'm worried that when I change 11 to an input to read it it's just dumb luck that the result happens to be it's previous output state.

2) I can't comment my code. Shift 3 gives a pound symbol instead of #. I've tried telling it I'm in Australia with a US keyboard in the internationisation in config but no luck.

User avatar
croston
Posts: 722
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Inverting a GPIO pin

Tue Oct 28, 2014 7:29 am

1) It is possible to GPIO.input() a channel setup as an output
2) Have you set up your keyboard using

Code: Select all

sudo raspi-config
You may need to reboot for the changes to take effect - I can't remember if this is the case or not.

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

Re: Inverting a GPIO pin

Tue Oct 28, 2014 1:45 pm

PlanB wrote:...
...

2) I can't comment my code. Shift 3 gives a pound symbol instead of #. I've tried telling it I'm in Australia with a US keyboard in the internationisation in config but no luck.
(For Raspbian OS)

sudo raspi-config

- Choose Internationalization menu
- Choose keyboard setup menu.
- If your exact keyboard is not on the list then choose one of the generic 101, 102 or 104 keyboards.

The following steps are Important! If you need US then you must choose US. Don't leave it set to UK.......

- You may need to scroll down and select [Other] to get back to the country of origin menu
- From country of origin menu select [English (US) ]
- Then from Keyboard layout: menu scroll to top of list and select - [ English (US) ]. Do not choose anything else unless you know exactly what you are doing!

- Complete the other menus then reboot.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Tue Oct 28, 2014 11:44 pm

Thnx people, # problem solved with a reboot after keyboard config. Re the pin inversion, what I really want to do is generate antiphase pwm. I can get PWM on one pin easily enough with
pwm=GPIO.PWM(12, 10) # setup pulse width mod on pin12 at 10 Hz
but how do I get a waveform on pin 11 going that is 180 degrees out of phase with the pin 12 waveform?

User avatar
aTao
Posts: 1095
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Inverting a GPIO pin

Tue Oct 28, 2014 11:57 pm

>)))'><'(((<

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Wed Oct 29, 2014 4:11 am

thnx klricks the rdp is a bit more user friendly than the SSH. Surely I can get antiphase out of the Pi without resorting to external hardware Tao?

User avatar
aTao
Posts: 1095
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Inverting a GPIO pin

Wed Oct 29, 2014 8:44 am

PlanB wrote:thnx klricks the rdp is a bit more user friendly than the SSH. Surely I can get antiphase out of the Pi without resorting to external hardware Tao?
Looks like it is possible, but..... what are you using the signal for?

The hardware PWM system has 2 channels which can be set to clock data out from a register called FIFO in the PWM system. If both channels are set to use FIFO then they each get data from alternate words in lock-step, this should be the root of your solution.
>)))'><'(((<

User avatar
joan
Posts: 15467
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Inverting a GPIO pin

Wed Oct 29, 2014 9:39 am

There may be far easier solutions, but they would very much depend on how the PWM signal is expected to vary. For instance, if the PWM signal has a fixed duty cycle there are trivial solutions.

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Wed Oct 29, 2014 11:09 pm

Just need a pair of antiphase square waves Joan (fixed 50% duty cycle) so very interested to hear of your trivials to do this please.

User avatar
aTao
Posts: 1095
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Inverting a GPIO pin

Wed Oct 29, 2014 11:31 pm

>)))'><'(((<

User avatar
joan
Posts: 15467
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Inverting a GPIO pin

Wed Oct 29, 2014 11:53 pm

10Hz 50% duty cycle anti-phase on gpios 7/8.

http://abyz.co.uk/videos/antiphase.webm

7 and 8 chosen as I can remember the values of 1<<7 and 1<<8.

Code: Select all

sudo pigpiod
$ pigs m 7 w
$ pigs m 8 w
$ pigs wvag 128 256 100000 256 128 100000
2
$ pigs wvcre
0
$ pigs wvtxr 0
7
http://abyz.co.uk/rpi/pigpio/

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Thu Oct 30, 2014 12:22 am

Wow, impressive! So I have to load up C to do that? Is there a way to do it with the python that comes with noobs?

User avatar
joan
Posts: 15467
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Inverting a GPIO pin

Thu Oct 30, 2014 12:31 am

PlanB wrote:Wow, impressive! So I have to load up C to do that? Is there a way to do it with the python that comes with noobs?
It really depends on how much fidelity you want from the inverse signal. The example I gave will be perfect.

Does the signal need to be perfect? Would it matter if the signal drifted from 10Hz or if the inverse signal sometimes lagged by a few milliseconds?

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Thu Oct 30, 2014 12:59 am

Pretty humble newby stuff. I'm just driving a bicolour 2 wire led on P1 pins 11 & 12. I can do green by holding pin 11 lo & PWMing 12, or red by taking pin 11 hi but I can't do orange (antiphase).

User avatar
joan
Posts: 15467
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Inverting a GPIO pin

Thu Oct 30, 2014 1:18 am

I'd just try a Python script first of all and see if the results are acceptable.

Code: Select all

...
import time
...
while True:
   write(GPIO1, 1) # Change write to what your Python module uses
   write(GPIO2, 0)
   time.sleep(0.1) # You probably want to change this to 0.01
   write(GPIO1, 0)
   write(GPIO2, 1)
   time.sleep(0.1) # You probably want to change this to 0.01

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Thu Oct 30, 2014 2:05 am

I forgot to mention the Pi is busy with some other processing intensive tasks so I really need to be using DMA style PWM, I can't afford to bog it down in a software loop.

User avatar
joan
Posts: 15467
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Inverting a GPIO pin

Thu Oct 30, 2014 1:52 pm

PlanB wrote:I forgot to mention the Pi is busy with some other processing intensive tasks so I really need to be using DMA style PWM, I can't afford to bog it down in a software loop.
Having a Python thread to do the loop shouldn't put a significant additional load on the system, I guess 1% or so.

If you are determined to use hardware timing I think RPIO.GPIO is likely to give the lightest additional load and should do what you want. The pigpio Python module will work but has an overhead of about 5% (because of the 200kps gpio sampling going on in the background).

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Inverting a GPIO pin

Thu Oct 30, 2014 11:27 pm

Much obliged Joan. Is there a go to document that has all this detail on what packages have how much overheads?

Return to “Beginners”