Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32 and ADXL343-Based Battery-Powered Accelerometer with SPI Communication

View and Edit Project In Cirkit DesignerImage of ESP32 and ADXL343-Based Battery-Powered Accelerometer with SPI Communication

Circuit Documentation

Summary

This circuit involves an ESP32 Devkit V1 microcontroller interfaced with an Adafruit ADXL343 accelerometer, a pushbutton, a rocker switch, resistors, capacitors, and a 12V 200Ah battery. The ESP32 is programmed to read data from the accelerometer and print it to the serial monitor. The circuit also includes voltage regulation using 7805 and 7808 voltage regulators.

Component List

  1. ESP32 Devkit V1

    • Description: Microcontroller with Wi-Fi and Bluetooth capabilities.
    • Pins: 3V3, GND, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, VIN
  2. Adafruit ADXL343

    • Description: 3-axis accelerometer.
    • Pins: VIN, 3.3V, GND, SCL, SDA/SDIO, SDO, CS, INT1, INT2
  3. Resistor

    • Description: 10k Ohm resistor.
    • Pins: pin1, pin2
    • Properties: Resistance: 10k Ohms
  4. Pushbutton

    • Description: Momentary pushbutton switch.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  5. Rocker Switch

    • Description: On/Off rocker switch.
    • Pins: 1, 2
  6. 12V 200Ah Battery

    • Description: Power source.
    • Pins: GND, 12V
  7. Electrolytic Capacitor

    • Description: Capacitor with 0.00047 Farads capacitance.
    • Pins: -, +
    • Properties: Capacitance: 0.00047 Farads
  8. Electrolytic Capacitor

    • Description: Capacitor with 0.001 Farads capacitance.
    • Pins: -, +
    • Properties: Capacitance: 0.001 Farads
  9. 7805

    • Description: 5V voltage regulator.
    • Pins: Vin, Gnd, Vout
  10. 7808

    • Description: 8V voltage regulator.
    • Pins: INPUT, GND, OUTPUT

Wiring Details

ESP32 Devkit V1

Adafruit ADXL343

Pushbutton

Resistor

Rocker Switch

12V 200Ah Battery

Electrolytic Capacitor (0.00047 Farads)

Electrolytic Capacitor (0.001 Farads)

7805

7808

Code Documentation

sketch.ino

#include "SparkFun_LIS331.h"
#include <SPI.h>

LIS331 xl;

static long timer = 0;
int16_t x, y, z;

void setup() 
{
  // put your setup code here, to run once:

  // Prepare SPI communication
  pinMode(5, OUTPUT);     // CS for SPI
  digitalWrite(5, HIGH);  // Make CS high
  pinMode(23, OUTPUT);    // MOSI for SPI
  pinMode(19, INPUT);     // MISO for SPI
  pinMode(18, OUTPUT);    // SCK for SPI
  SPI.begin();
  
  xl.setSPICSPin(5);         // This MUST be called BEFORE .begin() so 
                             //.begin() can communicate with the chip
                             
  xl.begin(LIS331::USE_SPI); // Selects the bus to be used and sets
                             //  the power up bit on the accelerometer.
                             //  Also zeroes out all accelerometer
                             //  registers that are user writable.
      
  Serial.begin(115200);
}

void loop(){
  if (millis() - timer > 1000){
    timer = millis();
    xl.readAxes(x, y, z);  // The readAxes() function transfers the
                           //  current axis readings into the three
                           //  parameter variables passed to it.

    Serial.println("X, Y, Z:");
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
    Serial.println(" ");

    Serial.println("G-RATING:");
    Serial.println(xl.convertToG(100,x)); // The convertToG() function
    Serial.println(xl.convertToG(100,y)); // accepts as parameters the
    Serial.println(xl.convertToG(100,z)); // raw value and the current
    Serial.println(" ");                  // maximum g-rating.
  }
}

This code initializes the SPI communication and sets up the LIS331 accelerometer. It reads the accelerometer data every second and prints the raw values and G-ratings to the serial monitor.