Using I2C to read value from analog pin on arduino and sending it to raspberry pi. It returns weird numbers like 122 or 255

StackOverflow https://stackoverflow.com/questions/22134573

Question

SETUP:

MASTER DEVICE: Raspberry Pi Model B REV_2

SLAVE DEVICE: Arduino Uno REV_3

PROBLEM:

Whenever I enter "r" into the command line it returns a number that is completely off what it should be. For example, when I hook my jumper wire to the analog pin A0 up to 5V and press "r" on the command line it should return 5 volts. Well it returned 255. When I had the jumber wire hooked up to the 3.3V pin it returned 169.

NOTE:

The person who wrote this code did note something that I thought might be related to this issue. His words were as follows.....

"The setup function in the Arduino then sets up two callback functions thatwill be used. The function processMessage will be invoked whenever the on Receive event occurs. This will be whenever a command is sent from the Raspberry Pi. The other callback function, sendAnalogReading, is associated with the onRequest event. This occurs when the Raspberry Pi requests data and will read the analog value divide it by four to make it fit into a single byte, and then send it back to the Raspberry Pi."

I have no idea what he means by dividing the value to make it fit into a single byte. Is this why i'm getting weird numbers back? Can someone explain this.

Just to make this thread more clear here is my setup and the output that is being displayed from issuing multiple commands.

sudo python ardu_pi_i2c.py //ran my program

First scenario, I hooked the jumper wire from pin A0 to GRD on the arduino. Then I chose the "r" option and it gave me "0"

Second scenario I hooked the jumper wire from pin A0 to 5V on the arduino. Then I choose "r" and it gave me "255"

Third scenario I hooked the jumper wire from pin A0 to 3.3V. It gave me 171.

Fourth scenario I hooked the jumper wire from pin A0 to the negative side of the LED and it gave me "0"

Fifth scenario I hooked the jumper wire from pin A0 to the positive side of the LED and it gave me "105".

Since the first and fourth scenarios seem to work alight i'm curious why the other numbers where way off and if they have some actual meaning to them.

enter image description here

PSUEDO CODE:

//Creates instance of SMBus called bus
//Prompts user for command "l" (toggles led) or "r" (reads from Analog pin 0)
//if command is l it writes it to arduino and causes onReceive handler(processmessage)
//if command is r then request_reading function will be called
//this will call read_byte in SMBus library that causes the on Request event to be     invoked. 

PYTHON PROGRAM

import smbus
import time
bus = smbus.SMBus(1)
SLAVE_ADDRESS = 0x04
def request_reading():
    reading = int(bus.read_byte(SLAVE_ADDRESS))
    print(reading)
while True:
    command = raw_input("Enter command: l - toggle LED, r - read A0 ")
    if command == 'l' :
        bus.write_byte(SLAVE_ADDRESS, ord('l'))
    elif command == 'r' :
        request_reading()

ARDUINO PROGRAM

#include <Wire.h>
int SLAVE_ADDRESS = 0x04;
int ledPin = 13;
int analogPin = A0;
boolean ledOn = false;
void setup() 
{
    pinMode(ledPin, OUTPUT);
    Wire.begin(SLAVE_ADDRESS);
    Wire.onReceive(processMessage);
    Wire.onRequest(sendAnalogReading);
    Serial.begin(9600);
}
void loop()
{
}
void processMessage(int n){
  Serial.println("In processMessage");
  char ch = Wire.read();
    if (ch == 'l'){
       toggleLED();}}
void toggleLED(){
  ledOn = ! ledOn;
  digitalWrite(ledPin, ledOn);}
void sendAnalogReading(){
  Serial.println("In sendAnalogReading");
  int reading = analogRead(analogPin);
  Wire.write(reading >> 2);}

No correct solution

OTHER TIPS

I believe that the output is a number in the range of 0 - 255 (all the values that fit into one byte) corresponding to a range of 0 - 5 volts. So, in order to convert the number read to the voltage, you do

voltage = number * 5.0 / 255.

You will find that for number = 255, you get voltage = 5.0;
and for number = 171, you get voltage = 3.35

Sounds right to me. This means that the voltage on the +ve side of the LED is

105 * 5.0 / 255 = 2.05 V

Which is not a crazy value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top