Skip to content

Commit

Permalink
Add test for Adafruit sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
saturn691 committed May 22, 2024
1 parent 7c90e41 commit 776b9c1
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true,
}
16 changes: 8 additions & 8 deletions esp32-tests/adc_veer/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include <Arduino.h>

// Define the pins connected to the PCB
#define VB_PIN 34 // Analog pin for battery voltage
#define I5_PIN 35 // Analog pin for 5V current sense
#define IM_PIN 32 // Analog pin for motor current sense
#define VB_PIN 34 // Analog pin for battery voltage
#define I5_PIN 35 // Analog pin for 5V current sense
#define IM_PIN 32 // Analog pin for motor current sense

// Constants for voltage dividers and calibration
const float VOLTAGE_DIVIDER_RATIO =
0.167; // Example ratio based on 100k and 20k resistors
const float CURRENT_SENSE_RESISTOR = 0.1; // Example value in ohms
const float ADC_MAX_VOLTAGE = 3.3; // Maximum voltage for the ADC (ESP32)
const int ADC_RESOLUTION = 4096; // 12-bit ADC resolution
0.167; // Example ratio based on 100k and 20k resistors
const float CURRENT_SENSE_RESISTOR = 0.1; // Example value in ohms
const float ADC_MAX_VOLTAGE = 3.3; // Maximum voltage for the ADC (ESP32)
const int ADC_RESOLUTION = 4096; // 12-bit ADC resolution

void setup()
{
Serial.begin(115200);
analogReadResolution(12); // Set ADC resolution to 12-bit
analogReadResolution(12); // Set ADC resolution to 12-bit
}

void loop()
Expand Down
5 changes: 3 additions & 2 deletions esp32-tests/single_stepper_motor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void setup()
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}

void loop()
{
// Set motor direction clockwise
Expand All @@ -37,7 +38,7 @@ void loop()
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Wait a second
delay(1000); // Wait a second

// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
Expand All @@ -50,5 +51,5 @@ void loop()
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Wait a second
delay(1000); // Wait a second
}
19 changes: 19 additions & 0 deletions esp32/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp-wrover-kit]
platform = espressif32
board = esp-wrover-kit
framework = arduino
monitor_speed = 115200

lib_deps =
adafruit/Adafruit MPU6050
adafruit/Adafruit Unified Sensor
124 changes: 124 additions & 0 deletions esp32/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Basic demo for accelerometer readings from Adafruit MPU6050

// ESP32 Guide: https://RandomNerdTutorials.com/esp32-mpu-6050-accelerometer-gyroscope-arduino/
// ESP8266 Guide: https://RandomNerdTutorials.com/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino/
// Arduino Guide: https://RandomNerdTutorials.com/arduino-mpu-6050-accelerometer-gyroscope/

#include <Arduino.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void)
{
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens

Serial.println("Adafruit MPU6050 test!");

// Try to initialize!
if (!mpu.begin())
{
Serial.println("Failed to find MPU6050 chip");
while (1)
{
delay(10);
}
}
Serial.println("MPU6050 Found!");

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange())
{
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange())
{
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}

mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth())
{
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}

Serial.println("");
delay(100);
}

void loop()
{
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);

/* Print out the values */
// Serial.print(a.acceleration.x);
// Serial.print(" ");
// Serial.print(a.acceleration.y);
// Serial.print(" ");
// Serial.print(a.acceleration.z);
// Serial.print(" ");

Serial.print(g.gyro.x);
Serial.print(" ");
Serial.print(g.gyro.y);
Serial.print(" ");
Serial.print(g.gyro.z);
Serial.print(" ");

// Serial.print(temp.temperature);

Serial.println("");
delay(1000 / 60);
}

0 comments on commit 776b9c1

Please sign in to comment.