Advertisement
microrobotics

Pololu Zumo 32U4 OLED Robot

Apr 22nd, 2023
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Zumo 32U4 OLED Robot is a versatile robot platform with several built-in sensors, including an OLED display, an LSM303D accelerometer and magnetometer, an L3GD20H gyroscope, infrared proximity sensors, a reflectance sensor array, and encoders for motor control. The following Arduino code demonstrates how to use these sensors and display the information on the OLED screen.
  3.  
  4. First, make sure you've installed the necessary libraries for the Zumo 32U4:
  5.  
  6. Zumo32U4: https://github.com/pololu/zumo-32u4-arduino-library
  7. SSD1306Ascii: https://github.com/greiman/SSD1306Ascii
  8.  
  9. This code initializes and uses all the sensors available on the Zumo 32U4 OLED Robot. It reads the accelerometer, gyroscope, and magnetometer data from the IMU; proximity sensor data; line sensor data; and encoder counts. The sensor data is then displayed on the OLED screen.
  10.  
  11. To use this code, upload it to your Zumo 32U4 robot using the Arduino IDE, making sure you have installed the required libraries mentioned at the beginning. After uploading, the robot will display sensor data on the OLED screen. Note that this code is only for reading and displaying sensor data; you can expand upon it to incorporate specific behaviors, movements, or interactions based on sensor readings.
  12.  
  13. */
  14.  
  15. #include <Wire.h>
  16. #include <Zumo32U4.h>
  17. #include <SSD1306Ascii.h>
  18. #include <SSD1306AsciiWire.h>
  19.  
  20. // Create instances of the Zumo 32U4 classes
  21. Zumo32U4ButtonA buttonA;
  22. Zumo32U4ButtonB buttonB;
  23. Zumo32U4ButtonC buttonC;
  24. Zumo32U4Buzzer buzzer;
  25. Zumo32U4Encoders encoders;
  26. Zumo32U4IMU imu;
  27. Zumo32U4IRPulses irPulses;
  28. Zumo32U4LCD lcd;
  29. Zumo32U4LineSensors lineSensors;
  30. Zumo32U4Motors motors;
  31. Zumo32U4ProximitySensors proximitySensors;
  32.  
  33. // OLED display configuration
  34. #define OLED_I2C_ADDRESS 0x3C
  35. SSD1306AsciiWire oled;
  36.  
  37. void setup() {
  38.   // Initialize serial communication
  39.   Serial.begin(115200);
  40.   Wire.begin();
  41.  
  42.   // Initialize OLED display
  43.   oled.begin(&Adafruit128x64, OLED_I2C_ADDRESS);
  44.   oled.setFont(Adafruit5x7);
  45.  
  46.   // Initialize sensors
  47.   imu.init();
  48.   proximitySensors.initThreeSensors();
  49.   lineSensors.initThreeSensors();
  50.  
  51.   // Calibrate line sensors
  52.   lcd.clear();
  53.   lcd.print(F("Press A to"));
  54.   lcd.gotoXY(0, 1);
  55.   lcd.print(F("calibrate"));
  56.   buttonA.waitForButton();
  57.   calibrateLineSensors();
  58.  
  59.   // Initialize buzzer
  60.   buzzer.playFrequency(440, 200, 15);
  61.  
  62.   // Clear OLED display
  63.   oled.clear();
  64. }
  65.  
  66. void loop() {
  67.   readSensors();
  68.   displaySensorData();
  69.   delay(100);
  70. }
  71.  
  72. void readSensors() {
  73.   imu.read();
  74.   proximitySensors.read();
  75.   lineSensors.read(sensorValues);
  76. }
  77.  
  78. void displaySensorData() {
  79.   oled.clear();
  80.  
  81.   oled.print(F("Accel:"));
  82.   oled.print(imu.a.x);
  83.   oled.print(F(","));
  84.   oled.print(imu.a.y);
  85.   oled.print(F(","));
  86.   oled.println(imu.a.z);
  87.  
  88.   oled.print(F("Gyro:"));
  89.   oled.print(imu.g.x);
  90.   oled.print(F(","));
  91.   oled.print(imu.g.y);
  92.   oled.print(F(","));
  93.   oled.println(imu.g.z);
  94.  
  95.   oled.print(F("Mag:"));
  96.   oled.print(imu.m.x);
  97.   oled.print(F(","));
  98.   oled.print(imu.m.y);
  99.   oled.print(F(","));
  100.   oled.println(imu.m.z);
  101.  
  102.   oled.print(F("Prox:"));
  103.   oled.print(proximitySensors.countsFrontWithLeftLeds());
  104.   oled.print(F(","));
  105.   oled.print(proximitySensors.countsFrontWithRightLeds());
  106.   oled.print(F(","));
  107.   oled.println(proximitySensors.countsLeftWithLeftLeds());
  108.   oled.print(F(","));
  109.   oled.print(proximitySensors.countsLeftWithRightLeds());
  110.   oled.print(F(","));
  111.   oled.print(proximitySensors.countsRightWithLeftLeds());
  112.   oled.print(F(","));
  113.   oled.println(proximitySensors.countsRightWithRightLeds());
  114.  
  115.   oled.print(F("Line:"));
  116.   for (uint8_t i = 0; i < 3; i++) {
  117.     oled.print(sensorValues[i]);
  118.     if (i < 2) {
  119.       oled.print(F(","));
  120.     }
  121.   }
  122.   oled.println();
  123.  
  124.   oled.print(F("Enc:"));
  125.   oled.print(encoders.getCountsLeft());
  126.   oled.print(F(","));
  127.   oled.println(encoders.getCountsRight());
  128. }
  129.  
  130. void calibrateLineSensors() {
  131.   lcd.clear();
  132.   lcd.print(F("Calibrating..."));
  133.  
  134.   // Turn the robot in place for calibration
  135.   motors.setSpeeds(200, -200);
  136.   lineSensors.calibrate();
  137.   delay(1000);
  138.   motors.setSpeeds(-200, 200);
  139.   lineSensors.calibrate();
  140.   delay(1000);
  141.   motors.setSpeeds(0, 0);
  142.  
  143.   lcd.clear();
  144.   lcd.print(F("Done calibrating"));
  145.   delay(1000);
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement