0

i am trying to connect raspi 3 (master) with an Arduino Mega (slave). I have 4 analog sensors on the on the Arduino Pins A0 - A3.

I now want to be able to manually start and stop the readings from the Arduino. After the transmission is manually brought to an end the values should be transferred into a csv file.

For now i have only figured out how to get a singular reading from the Arduino. however the values are only between 0 and 255. How do i get the whole range to 1023?

The python script ist the following:

import smbus
import time
bus = smbus.SMBus(1)
SLAVE_ADDRESS = 0x08

def requestreading():
block = bus.read_i2c_block_data((SLAVE_ADDRESS), 0, 4)
print(block)

while True:
var = input("Press any key for reading: ")
requestreading()

The Arduino script looks like this:

#include <Wire.h>
int SLAVE_ADDRESS = 0x08;
int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;

void setup(){
  Wire.begin(SLAVE_ADDRESS);
  Wire.onRequest(sendAnalogReading);
}
void loop(){

}

void sendAnalogReading(){
  int reading1 = analogRead(analogPin1);
  int reading2 = analogRead(analogPin2);
  int reading3 = analogRead(analogPin3);
  int reading4 = analogRead(analogPin4);


  Wire.write(reading1);
      Wire.write(reading2);
      Wire.write(reading3);
      Wire.write(reading4);

}
  • Not really a Raspberry Pi specific question as it's actually about the Arduino, as far as I can tell. Might be better served asked somewhere else. However, here's a hint: the form of the wire.write() function you're using spits out a single unsigned byte (8-bits) but to get 1023 you need 10-bits so you need to send two bytes per sensor (at least). When you reassemble them on the Pi after you've received them take note of the endianess. – Roger Jones Feb 5 at 17:12

Your Answer

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

Browse other questions tagged or ask your own question.