简体   繁体   English

使用I2C从arduino上的模拟引脚读取值并将其发送到raspberry pi。 它返回奇怪的数字,如122或255

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

SETUP: 设定:

MASTER DEVICE: Raspberry Pi Model B REV_2 MASTER DEVICE:Raspberry Pi Model B REV_2

SLAVE DEVICE: Arduino Uno REV_3 奴役装置: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. 每当我在命令行中输入“r”时,它返回一个完全不应该的数字。 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. 例如,当我将跳线连接到模拟引脚A0高达5V并在命令行上按“r”时,它应返回5伏。 Well it returned 255. When I had the jumber wire hooked up to the 3.3V pin it returned 169. 好吧,它返回255.当我将jumber线连接到3.3V引脚时,它返回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." “然后Arduino中的setup函数会设置两个将被使用的回调函数。只要on Receive事件发生,函数processMessage就会被调用。只要从Raspberry Pi发送一个命令,它就会被调用。另一个回调函数sendAnalogReading,与onRequest事件相关联。当Raspberry Pi请求数据并将读取模拟值除以4以使其适合单个字节,然后将其发送回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 sudo python ardu_pi_i2c.py //运行我的程序

First scenario, I hooked the jumper wire from pin A0 to GRD on the arduino. 第一种情况,我将跳线连接到arduino上的引脚A0到GRD。 Then I chose the "r" option and it gave me "0" 然后我选择了“r”选项,它给了我“0”

Second scenario I hooked the jumper wire from pin A0 to 5V on the arduino. 第二种情况我将跳线连接到arduino上的引脚A0到5V。 Then I choose "r" and it gave me "255" 然后我选择“r”它给了我“255”

Third scenario I hooked the jumper wire from pin A0 to 3.3V. 第三种情况我将跳线从引脚A0连接到3.3V。 It gave me 171. 它给了我171。

Fourth scenario I hooked the jumper wire from pin A0 to the negative side of the LED and it gave me "0" 第四种情况我将跳线从引脚A0连接到LED的负极,它给了我“0”

Fifth scenario I hooked the jumper wire from pin A0 to the positive side of the LED and it gave me "105". 第五种情况我将跳线从引脚A0连接到LED的正极,它给了我“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. 由于第一个和第四个场景似乎有点工作,我很好奇为什么其他数字离开了,如果它们对它们有一些实际意义。

在此输入图像描述

PSUEDO CODE: PSUEDO代码:

//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 PYTHON计划

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 ARDUINO计划

#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);}

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. 我相信输出是0到255范围内的数字(所有值都适合一个字节),对应的范围是0到5伏。 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 ; 你会发现,对于number = 255 ,你得到voltage = 5.0 ;
and for number = 171 , you get voltage = 3.35 number = 171voltage = 3.35

Sounds right to me. 听起来对我来说。 This means that the voltage on the +ve side of the LED is 这意味着LED的+ ve侧的电压为

105 * 5.0 / 255 = 2.05 V

Which is not a crazy value. 这不是一个疯狂的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM