#include <ADC.h>
#define ADXL1002_PIN A9
#define ZERO_G_VOLTAGE 1.65 // in volts
#define SENSITIVITY 0.4 // in volts/g
ADC *adc = new ADC();
elapsedMicros sinceLastSample; // timer for sampling
const uint32_t samplingIntervalUs = 1; // sampling interval in microseconds
void setup() {
Serial.begin(2000000);
adc->adc0->setAveraging(1); // set number of averages
adc->adc0->setResolution(12); // set bits of resolution
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED); // change the conversion speed
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed
sinceLastSample = 0;
}
void loop() {
if (sinceLastSample >= samplingIntervalUs) {
sinceLastSample = 0; // reset the timer
int rawValue = adc->analogRead(ADXL1002_PIN);
float voltage = rawValue * 3.3 / 4095.0;
float acceleration = (voltage - ZERO_G_VOLTAGE) * (1000.0 / SENSITIVITY);
Serial.println(rawValue);
}
}