piacco
Posts: 6
Joined: Mon Oct 29, 2012 8:32 pm

Invert GPIO RxD pin (solved)

Tue Nov 06, 2012 8:25 pm

Hello,

I have connected my smart energy meter to my raspberry pi. The problem is that the port on my energy meter uses a reversed serial protocol (1=0 and 0=1), how can i invert the RxD GPIO-pin on my Raspberry pi?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11836
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Invert GPIO RxD pin

Tue Nov 06, 2012 9:35 pm

By using a hardware inverter to invert the signal, something that a real RS232 converter already does, (it converts a high level into a negative signal, and a low level into a positive signal).

Such an inverter can simply be built with a single transistor and two resistors, or you can use a chip with six of them, like a 74HC04 (powered with 3,3V).

Its not possible to invert the signal "with software", except when you "bit bang" (emulate) the whole UART.

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

Re: Invert GPIO RxD pin

Tue Nov 06, 2012 11:19 pm

How are you reading the data at the Pi end? In C you just invert each byte as it arrives, e.g.

outByte = inByte ^ 0xFF;

Would have to disable parity checking though.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11836
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Invert GPIO RxD pin

Wed Nov 07, 2012 12:06 pm

That wouldn't work, as start and stopbits are also inverted, so the whole protocol doesn't work!
in other words, its not just the data that is inverted.
You really need a hardware inverter.

User avatar
jojopi
Posts: 3075
Joined: Tue Oct 11, 2011 8:38 pm

Re: Invert GPIO RxD pin

Wed Nov 07, 2012 12:26 pm

You could implement an inverter using an interrupt on change input pin to drive an inverted output pin, and loop this on to the hardware UART. This is certainly easier than bit-banging your own UART, and will work to higher frequencies.

(There is little reason to begrudge a hardware inverter in a Pi project, but in a small project on an 8bit MCU, trading two IO pins for an external component might make sense.)

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

Re: Invert GPIO RxD pin

Wed Nov 07, 2012 2:08 pm

mahjongg wrote:That wouldn't work, as start and stopbits are also inverted, so the whole protocol doesn't work!
in other words, its not just the data that is inverted.
You really need a hardware inverter.
Yes, I hadn't thought about the start and stop bits. In that case I'd just handle the whole bit stream in software. Might be outside some peoples' comfort zone though! :D

piacco
Posts: 6
Joined: Mon Oct 29, 2012 8:32 pm

Re: Invert GPIO RxD pin

Wed Nov 07, 2012 3:29 pm

For reading data out of the energy meter i have used a usb cabel with a TTL-232R FTDI chip. With the program FTDI-prog i have only inverted the RxD pin and this works fine. But the problem with this cable is that it only work for a couple of hours and than the RPI will hang. So i thought when i use the GPIO pins to read the data out of the meter this problem should be solved. The problem of this solution is that i have to invert the RxD GPIO pin of the RPI.

With a little perl script i get the data from the energy meter the port settings are:
Baudrate 9600, Data bits 7, Parity Even, Stop bits 1

piacco
Posts: 6
Joined: Mon Oct 29, 2012 8:32 pm

Re: Invert GPIO RxD pin

Wed Nov 07, 2012 9:56 pm

piacco wrote:For reading data out of the energy meter i have used a usb cabel with a TTL-232R FTDI chip. With the program FTDI-prog i have only inverted the RxD pin and this works fine. But the problem with this cable is that it only work for a couple of hours and than the RPI will hang. So i thought when i use the GPIO pins to read the data out of the meter this problem should be solved. The problem of this solution is that i have to invert the RxD GPIO pin of the RPI.

With a little perl script i get the data from the energy meter the port settings are:
Baudrate 9600, Data bits 7, Parity Even, Stop bits 1
So can someone tell me that if only use 74HC04 chip to invert the RxD pin this will work!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11836
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Invert GPIO RxD pin

Thu Nov 08, 2012 12:22 am

Yes using the 74HC04 (but not the HCT04) will work, if you power it with 3V3.

But you must not input more than 3V3 to the input of the 74HC04, so the RxD signal from the energy meter should be a 3V3 level.

But I'm a bit confused that all this is necessary, if you can connect the energy meters signal to the TTL-232R FTDI chip without inverting it, you should also be able to input it to the PI without inverting it!

Are you sure your previous efforts were done correctly?
The only "level issue" is the 5V to 3V3 level conversion, as the PI cannot handle 5V levels. If the energy meters signal outputs a 5V level, you should attenuate it to 3V, (with a resistor divider, or at the very least by putting a current limiting resistor (2K2 ... 10K) in series with it). Did you connect it to the right input (the RxD not the TxD) and is everything correct on the software side?

again, inverting a standard TTL serial (UART) signals isn't needed, only TTL to RS232 drivers (and obviously RS232 to TTL receivers) "invert" the levels (in the sense that a high logic level converts to -10V output, and a low logic level (0V) converts to a +10V output.
But all serial devices that use (TTL or 3V3) logic levels use the same polarity, the stop signal is high, (3.3 or 5V) and the startbit is low (0V) without exceptions!

That is why you can connect a PI's serial interface, to a microcontroller running on the same 3V3 voltage directly together, without any interface logic, or to a USB FTDI chip (provided it uses 3V3 levels)

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Invert GPIO RxD pin

Thu Nov 08, 2012 12:50 am

piacco wrote:Hello,

I have connected my smart energy meter to my raspberry pi. The problem is that the port on my energy meter uses a reversed serial protocol (1=0 and 0=1), how can i invert the RxD GPIO-pin on my Raspberry pi?
Why do you need an inverter?

If the levels are TTL or 3V3, when sitting idle then you should not need to do anything beyond making sure if it is 5V it is reduced down to 3V3, which could be done by a level translater or at least a couple of resistors to test it first.

Make sure you have TX from Energy metrer connected to RX on Pi. Then check baud rate, no of bits and parity match.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

piacco
Posts: 6
Joined: Mon Oct 29, 2012 8:32 pm

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 8:32 am

Can i easily test the RxD pin to check if its working properly, because i think the gpio-pin is damaged :(

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11836
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 9:29 am

A loopback.

connect RxD to TxD on the PI, send out data from the TxD, and it should be received at the RxD.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 9:36 am

piacco wrote:Can i easily test the RxD pin to check if its working properly, because i think the gpio-pin is damaged :(
What does the pin's voltage read when nothing is connected?

If you have disabled the OS from using the serial port /dev/ttyAMA0 from being used as a login tty so is avilable for use and you have a programme to send and read a character at time link TXD to RXD.
Then check with your programme that the same character is received as sent.

This is known as a local loopback test. You probably either have your pins round the wrong way or a baud rate or bit pattern mismatch

If you have other known working connections with working devices is another test

Using a scope is another test depends on your available tools.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

piacco
Posts: 6
Joined: Mon Oct 29, 2012 8:32 pm

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 8:21 pm

techpaul wrote:
piacco wrote:Can i easily test the RxD pin to check if its working properly, because i think the gpio-pin is damaged :(
What does the pin's voltage read when nothing is connected?

If you have disabled the OS from using the serial port /dev/ttyAMA0 from being used as a login tty so is avilable for use and you have a programme to send and read a character at time link TXD to RXD.
Then check with your programme that the same character is received as sent.

This is known as a local loopback test. You probably either have your pins round the wrong way or a baud rate or bit pattern mismatch

If you have other known working connections with working devices is another test

Using a scope is another test depends on your available tools.
Its working :D :D

The serial port was enabled for OS, to disable the Serial Port Login the file /etc/inittab must be changed by adding a # character to the beginning of the following rule
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100,
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100


For inverting the RxD pin i used a 74HCT04 chip. With two resistors of 10k the "level issue" of 5V is also solved.

Tanks for everyone who helped me.

Greetings,
Albert

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11836
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 8:46 pm

Still don't understand why inverting was necessary, but okay if it works...

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Invert GPIO RxD pin

Fri Nov 09, 2012 8:55 pm

piacco wrote:
techpaul wrote:
piacco wrote:Can i easily test the RxD pin to check if its working properly, because i think the gpio-pin is damaged :(
What does the pin's voltage read when nothing is connected?

If you have disabled the OS from using the serial port /dev/ttyAMA0 from being used as a login tty so is avilable for use and you have a programme to send and read a character at time link TXD to RXD.
Then check with your programme that the same character is received as sent.

This is known as a local loopback test. You probably either have your pins round the wrong way or a baud rate or bit pattern mismatch

If you have other known working connections with working devices is another test

Using a scope is another test depends on your available tools.
Its working :D :D

The serial port was enabled for OS, to disable the Serial Port Login the file /etc/inittab must be changed by adding a # character to the beginning of the following rule
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100,
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100


For inverting the RxD pin i used a 74HCT04 chip. With two resistors of 10k the "level issue" of 5V is also solved.

Tanks for everyone who helped me.

Greetings,
Albert
Sounds like the manufacturer of your FTDI cable has sillily used an extra programmed function within the FTDI chips and inverted TXD (your RXD) when it did not need to be. They may have inverted RXD (your TXD) as well within the device

Whenever I use the FTDI chips like FT232RL, I never invert the RXD or TXD so they connect with no need for inverters
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Freemann
Posts: 10
Joined: Wed Jun 27, 2012 8:20 pm

Re: Invert GPIO RxD pin (solved)

Tue Feb 12, 2013 2:51 pm

@Piacco/Albert

could you mail me you're solution (howto connect the components) at

marco [email protected]@rtje frijmann puntjes nederlandseExtensie

Hi-Z
Posts: 29
Joined: Wed Jan 02, 2013 8:19 pm
Location: Canuckistan

Re: Invert GPIO RxD pin

Tue Feb 12, 2013 3:44 pm

mahjongg wrote:Yes using the 74HC04 (but not the HCT04) will work, if you power it with 3V3.

But you must not input more than 3V3 to the input of the 74HC04, so the RxD signal from the energy meter should be a 3V3 level.
A 74HC04 that is powered by 3v3 will work just fine with TTL (0~5v) signal levels as an input to the buffers. Granted it's not proper hardware design, but it does work as a poor man's level shifter/inverter.

By the same reasoning, a 74HC04 powered at 5v can take a 3v3 input and shift it to a TTL level output. Again, it's a poor man's level shifter that will work in a pinch but not recommended for a permanent design.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Invert GPIO RxD pin (solved)

Tue Feb 12, 2013 7:06 pm

Unless the meter was actually outputting RS-232 levels where >= +3v is logic 0, <= -3v is logic 1.

Enjoy!
Bill

gawerra
Posts: 1
Joined: Tue Feb 19, 2013 2:12 am

Re: Invert GPIO RxD pin (solved)

Tue Feb 19, 2013 2:27 am

I used an 74HC14 interfacing between an LV-MaxSonar®-EZ1 and the UART RD. This gives a nice clean signal.

LV-MaxSonar®-EZ1 TX --> 74HC14 Pin 1
74HC14 Pin 2 --> Raspberry PI Header Pin 10

Remember to hook the 74HC14 to the 3.3V on Header Pin1 of Raspberry PI.

hgestel
Posts: 5
Joined: Mon Nov 19, 2012 2:42 pm

Re: Invert GPIO RxD pin (solved)

Fri Mar 15, 2013 6:28 pm

Trying to connect P1 Rj11 to raspberry I used a CN-104 cable but now way to get proper data.
lsusb gives Pl2303 067b:2303

Is there a software way to reprogram the profilic so RxD will be inverted?
(I guess not, but you never know)
Like I read here I have to invert the RxD signal with a 74HC04, does anybody have a schematic?
wonder where you get the 3.3 V.

rgds

Henry

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Invert GPIO RxD pin (solved)

Fri Mar 15, 2013 10:46 pm

hgestel wrote:Trying to connect P1 Rj11 to raspberry I used a CN-104 cable but now way to get proper data.
lsusb gives Pl2303 067b:2303

Is there a software way to reprogram the profilic so RxD will be inverted?
(I guess not, but you never know)
Like I read here I have to invert the RxD signal with a 74HC04, does anybody have a schematic?
wonder where you get the 3.3 V.

rgds

Henry
NO

If you are using the CN-104 on your PC then that is coming out of the PC inverted at +12V and -12V levels, DO NOT connect that directly to your Pi !!!

As the cable is producing RS232 levels (+12V and -12V), use a MAX3232 or equivalent RS232 interface device to connect from RS232 to Pi UART at 3V3 levels.

Let me repeat do NOT use a 74 series chip to connect RS232 to PI
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

danjperron
Posts: 3330
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Invert GPIO RxD pin (solved)

Sat Mar 16, 2013 12:09 am

The simplest thing to do is to use a transistor or a mosfet to invert the signal. It will also protect the RPi.

This is a small schematic to implement a transistor. The diode will protect the inverse voltage.

Since you using the transistor in switch mode you could use the same resistor value for both but use big value too limit the current. The transistor will start to switch on around 0.6 V. when it turn on it will drop the voltage at the collector around 0.2V for low current.

The RXD will be connected directly at GPIO-15 (p1-10).

Image

Daniel

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Invert GPIO RxD pin (solved)

Sat Mar 16, 2013 1:27 am

simpler and smaller component size for a RS232 converter that does both ways in a small chip and few caps. Deals with all the hassles and protection.

Especially if you have invert problem this happens BOTH ways.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

hgestel
Posts: 5
Joined: Mon Nov 19, 2012 2:42 pm

Re: Invert GPIO RxD pin (solved)

Sat Mar 16, 2013 10:10 am

techpaul wrote:simpler and smaller component size for a RS232 converter that does both ways in a small chip and few caps. Deals with all the hassles and protection.

Especially if you have invert problem this happens BOTH ways.
Can you explain more?

I want to use the usb (usb hub connected to rpi) for s/w reason e.g. /dev/ttyUSBX is used as input.
I don`t want to make it more complex.
On one site Rj11 from smart meter other site usb. What goes in between?

Rgds
Henry

Return to “Interfacing (DSI, CSI, I2C, etc.)”