How could I read data from UNO with IIC block

Issue #468 resolved
Tom Ming created an issue

Hi @John Maloney , I connect three Potentiometers to A0 -A2 on UNO, and programs as below.

#include <Wire.h>

#define I2C_ADDRESS 0x10 // IIC address

void setup() {
  Wire.begin(); // init IIC
  Serial.begin(9600); // ini serial 
}

void loop() {
  int potValue1 = analogRead(A0); // 读取电位器1的数值
  int potValue2 = analogRead(A1); // 读取电位器2的数值
  int potValue3 = analogRead(A2); // 读取电位器3的数值

  // 将三个电位器的数值打包成一个字节数据
  byte data[3];
  data[0] = map(potValue1, 0, 1023, 0, 255);
  data[1] = map(potValue2, 0, 1023, 0, 255);
  data[2] = map(potValue3, 0, 1023, 0, 255);

  // 发送数据给ESP32
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(data, 3);
  Wire.endTransmission();

  // 打印数据到串口监视器
  Serial.print("Potentiometer 1: ");
  Serial.print(potValue1);
  Serial.print("  Potentiometer 2: ");
  Serial.print(potValue2);
  Serial.print("  Potentiometer 3: ");
  Serial.println(potValue3);

  delay(100); // 延时一段时间
}

I connect UNO(A4 A5 GND) to ESP32(GPIO21 GPIO22 GND).

How could I read data from UNO using IIC blocks?

Thanks

Comments (8)

  1. John Maloney repo owner

    I have never done this, but it should be possible. To use I2C, you will need to make the Arduino Uno act as an I2C slave and program it to respond to requests from the ESP32.

    Here are some links that may help:

    https://wiki-content.arduino.cc/en/Tutorial/LibraryExamples/MasterWriter https://deepbluembedded.com/arduino-i2c-slave/

    It might be easier to communicate using serial. To do that you would need to set both boards to the same serial baud rate. You connect the Rx pin of one board to the Tx of the other and vice versa. In that case, you might program the Arduino to repeatedly write the three potentiometer values to Serial in some format -- perhaps as three numbers per line separated by spaces. The ESP32 then needs to discard bytes until it sees a newline, then read and save bytes into a list until it sees another newline. That will give it one set of readings. The bytes can be converted into a string and the "split" block can be used to break them into three number strings and finally those number strings can be convert into actual integer values.

    But either approach should work.

  2. Tom Ming reporter

    Hi @John Maloney , Thanks for your quick reply.

    Turgut's example works.

    For me,The Arduino code is below.

    // I2C Slave  Transmitter
    // by Turgut Guneysu
    //
    // Used in demo of micro:bit to Arduino I2C comms
    // Receiver will print all characters received.
    // Transmitter will use a 32 byte buffer,
    //  but messages can be shorter by placing a \n at the end.
    
    #include <Wire.h>
    void setup() {
      Wire.begin(13);               // join i2c bus with address #13
      //Wire.onReceive(receiveEvent); // register event
      Wire.onRequest(requestEvent); // register event
      Serial.begin(9600);           // start serial for output
      Serial.println("Ready...");
    }
    
    void loop() {
    }
    
    //void receiveEvent(int howMany) {    // I2C Receive Event Handler
    //  char c;
    //  Serial.println(howMany);
    //  while (Wire.available()) {
    //    c = Wire.read();
    //    Serial.print(c);
    //  }
    //  Serial.print(" / LAST char: ");   // Also print the last char as DEC
    //  Serial.print(c);
    //  Serial.print(" = ");
    //  Serial.println(c, DEC);
    //}
    
    void requestEvent() {               // I2C Transmit event Handler
      //  char response[32] = "Hello from Arduino.\n";
      //Wire.write(response);             // respond with \n terminated message
      String str = String(analogRead(A0)) + String(",") + String(analogRead(A1)) + String(",") + String(analogRead(A2)) + String("\n");
      char charArray[str.length() + 1]; // 字符数组
      str.toCharArray(charArray, sizeof(charArray)); // 将String对象转换为字符数组
      Wire.write(charArray);
    }
    

    The MicroBlocks blocks is there.

    As you say, It is easier to communicate using serial.

    Thank you and @Turgut.

  3. Log in to comment