I used Arduino UNO to covert pressure sensor signal of (0-1023) to (0-255) and took out put from pin 9 as input to Raspberry pi. I can see data vary in range of 0-255 as I run serial monitor of arduino . but Rsapberry pi doesn't read the input. could you please help with my python code?

import RPi.GPIO as GPIO

GPIO.setup(19, GPIO.IN)
while True:
    input_value = GPIO.input(19)
    print str(input_value)
  • You need to realize that there are certain differences between the arduino and the Pi. For one the Pi does not have analog inputs to directly read this sensor, see here. What the current code is doing, is to repeatedly read a single digital input pin (one bit that is) and outputting it. It will never show something in the range of 0..255.
    – Ghanima
    Aug 31 '16 at 8:42
  • are there any way to show in such a range? I never inter communicated with Arduino & RPI.
    – user52747
    Aug 31 '16 at 8:50
  • 1
    I really can provide no better advise than to study the basics of the Pi's operation. There are plenty of tutorials out there covering how to interface it via the GPIO pins.
    – Ghanima
    Aug 31 '16 at 8:56

It sounds like what you are doing on the arduino is analogRead(), converting the range from 0-1024 to 0-255 then using analogWrite().

What you are doing is converting a analog signal to a PWM signal. The analogWrite() function is badly named in the arduino sdk. It does not write an analog signal to a pin. Instead it sends a series of pulses, with varying width between them where 0 is fully off and 1 is full on. In some devices (such as leds) this mimics the effect of changing the voltage. Other devices such as servos and motor controllers actually expect a PWM signal to function correctly. There is no way without addition hardware for an arduino UNO to generate a true analog signal.

Now, in electronics analog to digital converters and digital to analog converters convert a signal from a min to a max voltage in so many steps, 1024 in the case of the arduino ADC. Even if analogWrite produced a true analog signal, its range is 0-255, but the min and max voltage would still likely be the same - just with less resolution so all you are doing is lowering the resolution of the signal rather then reducing its voltage.

Now, the pi has no ADC built into it so you need some sort of external ACD to read an analog signal, there are many such chips about and you can skip the arduino UNO entirely by using one, without having to worry about coding it at all - you can just worry about coding on the pi.

Assuming you still want to use the arduino as a ADC you are going to need to talk to the pi over a protocol it understands. There are many of them to pick from: serial (UART), i2c or spi.

WARNING: the arduino UNO is a 5v device dy default, its pins will read/write signale at 5v. The raspberry pi can only accept 3.3v MAX on a pin. Sending a 5v signal to it will damage the pi. You can use a logic level converter to shift from 5v to 3.3v. Alternatively, you can run the arduino off 3.3v (by supplying 3.3v to the pin label 5v (and bypassing the 5v reg onboard) or by replacing the voltage reg with a 3.3v reg, at which point the pins will read/write 3.3v max.

Serial is probably the easiest to get started with. You will want to change your sketch basically; value = andlogRead() then Serial.println(value) and finally a small delay(500) to stop it overwhelming the serial line.

From the pi you can read these values with any serial reading application (picocom, screen, minicom) on the /dev/ttyAMA0 device file:

picocom -b 9600 /dev/ttyAMA0

Ensuring the baud rate matches the rate set in your sketch (9600 is a good value to start with). Use ctrl+a then ctrl-x to exit. This is useful for debugging the output from the arduino.

You can then use pythons pyserial module to open the serial port read a line and do what ever you want with the value.

  • thank you a lot. but I want to read more about this confusion in PWM and analog, I thought Arduino pin 9 output is digital signal and then RPI was taken that by GPIO 19.
    – user52747
    Aug 31 '16 at 9:13
  • PWM is a digital signal, the data is encoded in the width of the pulses. You can actually read the PWM pulse width from the pi, but it is not very accurate as it requires precise timing, which linux cannot do - the result is you can easily miss pulses. When you do a GPIO.input you are measuring the point in time value, it will either be 0 or 1. With PWM this is meaning less and simply probability based on the actual value you will get. If you do this frequently enough, and measure the time between reading a 1 and 0 you can calculate the pulse width. But you can easily miss pulses. Aug 31 '16 at 10:14
  • ok. I connect analog sensor and analogRead(A0) , then install picocom and what I see is now Terminal ready in RPI, but values not showing up. but serial monitor shows values 0..255..in arduino side. why is that?
    – user52747
    Aug 31 '16 at 11:02
  • Serial lines are designed for connecting a single device to one other device. The arduino serial is shared between the the serial pins (pins 0 and 1) and the chip that connects the arduino to the USB. You have two options, use the rx and tx pins connected to the raspberry pis tx and rx lines (tx to rx and rx to tx) while the usb is not connected to anything then read from /dev/ttyAMA0. Or plug the USB into the pi and read from /dev/ttyACM0 or /dev/ttyUSB0, which will talk to the arduino over the usb rather then the inbuilt serial lines on the pi. Aug 31 '16 at 11:08
  • 1
    Beware you cannot connect the serial lines from the pi to the Arduino directly. You need a logic level shifter or something similar to go from 5V to 3.3V. I've done this by accident once and (luckily) it did not damage the pi but it does not work -- you just get garbage.
    – goldilocks
    Aug 31 '16 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.