User avatar
phantomfreak
Posts: 12
Joined: Thu May 31, 2012 7:02 pm
Location: Rotherham, South Yorkshire, UK.

Easy Arduino - Raspberry Pi serial over GPIO

Mon Nov 12, 2012 5:01 pm

I created really simple level shifter for connecting a Raspberry PI GPIO Serial (pins 6 & 8) to the Arduino serial pins (0 & 1) using transistors (I didn't have a level shifting IC to hand.

Image

You don't technically need to raise the Pi TX to 5V (for the Arduino) but I thought it better practice to do so, and it prevents damage through wiring to the wrong Arduino pin (i.e. if you wanted to use Software Serial, but accidentally set pin high).

Remember to do the following...

Hash out the following line in /etc/inittab:

#TO:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Edit out the following entries in the line in /boot/cmdline.txt:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

And tie the ground of the Arduino to the ground of the Pi, otherwise it won't work!

To test, I'd reccomend uploading a sketch (see below) to the Arduino which reads a byte and returns something to the Pi then using 'Minicom -b 9600 -o -D /dev/AMA0' any keypress will return something. You'll likely have to install minicom (sudo apt-get minicom).

Code: Select all

byte num = 0;

void setup ()
{
 Serial.begin (9600); 
}

void loop ()
{
  if ( Serial.available () )
  {
    num = Serial.read ();
    Serial.print ( "I recieved : " );
    Serial.println ( num, DEC );
  }
}
NOTE: There is always a chance of damaging the Raspberry Pi pins if something is mis-wired since they are not 5V tollerent. BE CAREFUL.
Model-B (rev.1 & rev.2)
Model-A
Motorola Atrix Lapdock
Pibow (beta version)
Adafruit Cobbler
Humble Pi
7dayshop.com WiFi (RTL8188CUS)
7dayshop.com 8GB Class 10 SDHC
Arduino Uno (rev.3) via homebrew level shifter
twitter.com/phantomfreak

User avatar
phantomfreak
Posts: 12
Joined: Thu May 31, 2012 7:02 pm
Location: Rotherham, South Yorkshire, UK.

Re: Easy Arduino - Raspberry Pi serial over GPIO

Mon Nov 12, 2012 5:35 pm

I just wanted to leave a quick additional note... This was purely designed for level shifting, it was not an intention to conenct the arduino to the GPIO serial port for programming. Two things prevent this...

1) The Arduino IDE currently won't recognise the internal serial port, at all, this may change in the future though.

2) The Arduino IDE requires the reset pin to be connected somehow (to reset the ATMega328 before programming and after), but I have yet to figure out how this would connect in this case.

Hope everyone finds this useful, it's definitely lit up my Twitter (@PhantomFreak), thanks to a retweet from @Raspberry_Pi.
Model-B (rev.1 & rev.2)
Model-A
Motorola Atrix Lapdock
Pibow (beta version)
Adafruit Cobbler
Humble Pi
7dayshop.com WiFi (RTL8188CUS)
7dayshop.com 8GB Class 10 SDHC
Arduino Uno (rev.3) via homebrew level shifter
twitter.com/phantomfreak

AngusP
Posts: 5
Joined: Tue Jul 17, 2012 9:47 pm
Location: Basingstoke

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Jan 03, 2013 12:48 am

Hang on - in the example scetch did you mean

Code: Select all

Serial.begin("9600");
or

Code: Select all

Serial.begin('115200');
?? I'm just wondering, as the baud rates should be the same, at least I think...
Thanks, Angus.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Jan 03, 2013 1:05 am

And tie the ground of the Arduino to the ground of the Pi, otherwise it won't work!
I was wondering about that. Didn't think it would be so simple! :?
note: I may or may not know what I'm talking about...

User avatar
phantomfreak
Posts: 12
Joined: Thu May 31, 2012 7:02 pm
Location: Rotherham, South Yorkshire, UK.

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Jan 03, 2013 1:43 am

AngusP wrote:Hang on - in the example scetch did you mean

Code: Select all

Serial.begin("9600");
or

Code: Select all

Serial.begin('115200');
?? I'm just wondering, as the baud rates should be the same, at least I think...
Thanks, Angus.
If you red through more carefully I tell you to quote out the lines containing 115200. These lines would provide a serial debug interface, but prevent all othe use of serial.

Code: Select all

Hash out the following line in /etc/inittab:
#TO:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Edit out the following entries in the line in /boot/cmdline.txt:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
Minicom command listed is set to 9600 to match.

Code: Select all

Minicom -b 9600 -o -D /dev/AMA0
Yes, baud rates should be the same, I'm just in the habbit of working at 9600. However, should work just fine at 115200 as the transistors I used are rated up to 300MHz, but alas I've been that busy I haven't tested at the higher rate yet.

PS - Sorry for late reply, I didn't recieve an alert to your post via e-mail.
Model-B (rev.1 & rev.2)
Model-A
Motorola Atrix Lapdock
Pibow (beta version)
Adafruit Cobbler
Humble Pi
7dayshop.com WiFi (RTL8188CUS)
7dayshop.com 8GB Class 10 SDHC
Arduino Uno (rev.3) via homebrew level shifter
twitter.com/phantomfreak

User avatar
phantomfreak
Posts: 12
Joined: Thu May 31, 2012 7:02 pm
Location: Rotherham, South Yorkshire, UK.

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Jan 03, 2013 1:46 am

toxibunny wrote:
And tie the ground of the Arduino to the ground of the Pi, otherwise it won't work!
I was wondering about that. Didn't think it would be so simple! :?
Yes, it is that simple. If you think of a transistor or mosfet, you would normally tie the grounds together there too. Needed to complete the circuit after all.
Model-B (rev.1 & rev.2)
Model-A
Motorola Atrix Lapdock
Pibow (beta version)
Adafruit Cobbler
Humble Pi
7dayshop.com WiFi (RTL8188CUS)
7dayshop.com 8GB Class 10 SDHC
Arduino Uno (rev.3) via homebrew level shifter
twitter.com/phantomfreak

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: Easy Arduino - Raspberry Pi serial over GPIO

Mon Feb 11, 2013 5:55 pm

The Pi TX to arduino RX circuit is best being replaced by just a wire. The emitter base voltage drop means that the arduino will only be receiving a voltage of 3.3 - 0.7 = 2.6 V for a logic one. That will not be very reliable.

User avatar
phantomfreak
Posts: 12
Joined: Thu May 31, 2012 7:02 pm
Location: Rotherham, South Yorkshire, UK.

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 1:06 am

Grumpy Mike wrote:The Pi TX to arduino RX circuit is best being replaced by just a wire. The emitter base voltage drop means that the arduino will only be receiving a voltage of 3.3 - 0.7 = 2.6 V for a logic one. That will not be very reliable.
You must have misread, it's 5 - 0.7 = 4.3V recieved by the Ardiuino RX pin (the 3.3V is just switching the 5V). Plus, the Arduino has the strange problem that it keeps it's RX pin high, which could, in theory kill the Pi's TX pin. Using the transistor protects the Pi's TX pin from the 5V coming out of the RX pin. I know this sounds really strange, but test it with a meter, you'll see.
Model-B (rev.1 & rev.2)
Model-A
Motorola Atrix Lapdock
Pibow (beta version)
Adafruit Cobbler
Humble Pi
7dayshop.com WiFi (RTL8188CUS)
7dayshop.com 8GB Class 10 SDHC
Arduino Uno (rev.3) via homebrew level shifter
twitter.com/phantomfreak

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 10:05 am

You must have misread, it's 5 - 0.7 = 4.3V recieved by the Ardiuino RX pin (the 3.3V is just switching the 5V).
The diagram you posted at the start of this thread shows the Pi's output (Pi TX) going to the base of a transistor, this never gets higher than 3V3. Yes the collector goes to 5V but the configuration is an emitter follower. Therefore the emitter can never go higher than the voltage on the base minus the Vbe. So how do you get 4.3V on the emitter of the transistor to feed to the arduino?
It's physics Jim but not as we know it.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 11:16 am

Even simpler, direct from Rpi TX to Arduino RX. A voltage divider using a 10k and an 18k resistor from Arduino TX to Pi RX. Works for me at 115200 b/s......

The Pi and Arduino are both very high input impedance, the 'pull' across a voltage divider is negligable.

I'm with Grumpy, the 2n2222 is a bipolar transistor, you'll get whatever is on the base less a silicon junction voltage drop. 0.6-0.7 volts ish.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 1:06 pm

Old thread surfacing, however:

Even simpler: Make life easy for yourself and just use USB and stop all the faffing about with the on-board serial port.

A Pi can power an Arduino without any issues whatsoever. Also Arduino developmen on the Pi via USB "just works" too. (Although the newer Arduinos - Uno, etc. may not be immediately recognised by older Arduino IDEs as they use /dev/ttyACM0 rather than /dev/ttyUSB0)

Image

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 1:26 pm

My arduino cost less than £10, is built on a lump of stripboard and doesn't have USB.......

Image
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 1:30 pm

pluggy wrote:My arduino cost less than £10, is built on a lump of stripboard and doesn't have USB.......

Image
Technically speaking, it's not an Arduino...

But I presume you power it from 3.3v and just connect them directly...

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Tue Feb 12, 2013 4:32 pm

Its an arduino chip with a Uno bootloader, its a lot closer to an Arduino than some of the stuff that turns up on the Arduino forum. ;)

Some of the stuff I had powered from said 'not an arduino' didn't like 3.3 volts and it upset the clock frequency so it affected my timing stuff, so feeding it 5v and kludging the serial seemed the way to go.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

jointeffort
Posts: 2
Joined: Fri Feb 15, 2013 1:33 pm

Re: Easy Arduino - Raspberry Pi serial over GPIO

Fri Feb 15, 2013 1:36 pm

Hi,

I'm new to Raspi and Arduino. I want to connect my Raspi (Model B) to Arduino. This post warns for attaching 5V to the Raspi GPIO pins. I thought model B runs on 5V, or not?

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

Re: Easy Arduino - Raspberry Pi serial over GPIO

Fri Feb 15, 2013 1:41 pm

jointeffort wrote:Hi,

I'm new to Raspi and Arduino. I want to connect my Raspi (Model B) to Arduino. This post warns for attaching 5V to the Raspi GPIO pins. I thought model B runs on 5V, or not?
No, the logic works at 3V3 (all the gpios etc.)

lardconcepts
Posts: 43
Joined: Mon Feb 04, 2013 11:32 am

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Feb 21, 2013 5:17 pm

I've got an OLED which says it'll run on 3v-5v. I've got a 3.3v and 5v rail breadboard.
The OLED needs to send a "busy" signal to the PI's GPIO pins (I'm using the GPIO library) so of course, I'm using 3.3v supply to the OLED to avoid frying my PI.

The OLED sort of works at 3.3v but there's something quirky going on so I'd like to try it at 5v, so if I took your diagram and replaced your 2N2222 with a 2N3904 I've got here, feeding the OLED's R/S pin into the base of the transistor and pushing the 3.3v rail through the collector, would that be "safe"? Or am I just hopelessly lost here? It's not high frequency or anything, literally just "high: I'm busy, low: I'm free".

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Feb 21, 2013 6:13 pm

lardconcepts wrote:I've got an OLED which says it'll run on 3v-5v. I've got a 3.3v and 5v rail breadboard.
The OLED needs to send a "busy" signal to the PI's GPIO pins (I'm using the GPIO library) so of course, I'm using 3.3v supply to the OLED to avoid frying my PI.

The OLED sort of works at 3.3v but there's something quirky going on so I'd like to try it at 5v, so if I took your diagram and replaced your 2N2222 with a 2N3904 I've got here, feeding the OLED's R/S pin into the base of the transistor and pushing the 3.3v rail through the collector, would that be "safe"? Or am I just hopelessly lost here? It's not high frequency or anything, literally just "high: I'm busy, low: I'm free".
Personally, for a simple input like that I'd just use a resistor divider... say 3.3K and 2.2K.

Code: Select all

  OLED ---> [ 2.2K ] ---+---> Pi GPIO
                        |
                       -+-
                       3.3K
                       -+-
                        |
                       /// 0v
For lots of them there are commercial "breakout boards", but IIRC some of them just use resistors too!

As an excercise: work out the actual voltage going into the GPIO given a 5v input...


-Gordon
--
Gordons projects: https://projects.drogon.net/

lardconcepts
Posts: 43
Joined: Mon Feb 04, 2013 11:32 am

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Feb 21, 2013 7:03 pm

gordon@drogon.net wrote:As an excercise: work out the actual voltage going into the GPIO given a 5v input...
Now THIS is the kind of reply I like - not just the right answer, but something to make me think. And I'm afraid I'm so rusty I had to google it.

Basically, it appears to be
Vout = (Z2/(Z1+Z2))*Vin
Vout = (2200/(2200+3300))*5
I make that 3 safe and Pi-friendly volts. If I got them the wrong way round, then it's an even safer 2 volts.

Either way - thanks!

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Easy Arduino - Raspberry Pi serial over GPIO

Thu Feb 21, 2013 7:24 pm

lardconcepts wrote:
gordon@drogon.net wrote:As an excercise: work out the actual voltage going into the GPIO given a 5v input...
Now THIS is the kind of reply I like - not just the right answer, but something to make me think. And I'm afraid I'm so rusty I had to google it.

Basically, it appears to be
Vout = (Z2/(Z1+Z2))*Vin
Vout = (2200/(2200+3300))*5
I make that 3 safe and Pi-friendly volts. If I got them the wrong way round, then it's an even safer 2 volts.

Either way - thanks!
Yup.

It's essentially ratios... Also think about the way round the resistors are too - you'll get the bigger voltage drop over the bigger resistor...

And if you can, learn to approximate too - see that 2.2/3.3 is about two thirds ... and two thirds of 5 is about 3 and a bit - so once you know you're in the right ball-park you can fine-tune it.

Well that's how my mind approaches it anyway! I'm actually very bad at mental arithmetic, but I can usually work stuff out from first principles.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “Automation, sensing and robotics”