Prerequisites
If not already done so already, install Arduino software and make basic connections, as described in page for MRMS ESP32: Arduino, IMU, eFuse, BT, WiFi, CAN Bus (mrm-esp32).
Task
We will read 3 analog outputs of the IMU: heading, roll, and pitch.
Pins
Connections
Additionally to the basic hardware connection, as depicted in home page for
MRMS ESP32: Arduino, IMU, eFuse, BT, WiFi, CAN Bus (mrm-esp32), we will connect the sensor,
ML-R IMU, 3 analog outputs (mrm-imu-an3). The image here shows just this board and the IMU, not the other parts of the basic connection (power supply and battery), which are also necessary.
Connect 0 and 3.3 V voltage inputs of
ML-R IMU, 3 analog outputs (mrm-imu-an3) to appropriate voltage Dupont pins. Connect sensor's "YAW" pin to ESP32 GPIO pin 36, "PIT" to 39, and "ROL" to 34.
Program
Here is the simplest code.
const float f = 360 / 4095.0;
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print(analogRead(36) * f);
Serial.print(" ");
Serial.print(analogRead(39) * f);
Serial.print(" ");
Serial.println(analogRead(34) * f);
}
First, about constant f. Maximum number of degrees measurable is 360. As ESP32 uses 12-bit ADC, the values are in range 0 - 4095 (2
12). Analog signal from the sensor might not reach maximum voltage, 3.3 V (ADC 4095). If You measure a smaller maximum value, change 4095 into Your value. When we read the analog value from the sensor, we have to divide it by 4095 in order to get percentage of the maximum value (if the input is 410, and maximum is 4095, 410 / 4095 yields 0.1, 10%). If we multiply this percentage with the maximum possible value, 360, we will get the degrees (10% of 360º is 36º). Hence, we shall multiply the analog value with f. We'll do so soon.
setup() starts serial interface with PC so that Serial.print() will work. In loop() we finally multiply ADC values with f and display the results: yaw (heading), pitch, and roll.
We could use any other of the 16 analog pins, as long as no other device uses them, but read the next paragraph.
Limitations
Some pins are not available or are limited in usage. It is also important to note the ADC anomaly for ESP32 and select the right firmware as the sensor's output value should not start from 0. If it starts, ESP32 will report 0 mm for the first about up to 70 mm. Check this list.