Skip to content

Commit c5a7a63

Browse files
authored
Add files via upload
1 parent b2af05c commit c5a7a63

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/7semi_ADXL335.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*!
2+
* @file 7semi_ADXL335.cpp
3+
* @brief Arduino library for the ADXL335 3-axis analog accelerometer
4+
* @details This library reads analog voltage from the X, Y, Z outputs of the ADXL335 sensor
5+
* and converts it into raw ADC values, voltage (V), or acceleration (g-force).
6+
* @author 7semi
7+
* @copyright (c) 2025
8+
*/
9+
10+
#include "7semi_ADXL335.h"
11+
12+
ADXL335_7semi::ADXL335_7semi(uint8_t xPin, uint8_t yPin, uint8_t zPin)
13+
: x_pin(xPin), y_pin(yPin), z_pin(zPin) {
14+
}
15+
16+
void ADXL335_7semi::begin() {
17+
pinMode(x_pin, INPUT);
18+
pinMode(y_pin, INPUT);
19+
pinMode(z_pin, INPUT);
20+
}
21+
22+
void ADXL335_7semi::readRaw(int &x, int &y, int &z) {
23+
x = analogRead(x_pin);
24+
y = analogRead(y_pin);
25+
z = analogRead(z_pin);
26+
}
27+
28+
void ADXL335_7semi::readVoltage(float &x, float &y, float &z) {
29+
int rawX, rawY, rawZ;
30+
readRaw(rawX, rawY, rawZ);
31+
32+
x = (rawX / 1023.0) * REF_VOLTAGE;
33+
y = (rawY / 1023.0) * REF_VOLTAGE;
34+
z = (rawZ / 1023.0) * REF_VOLTAGE;
35+
}
36+
37+
void ADXL335_7semi::readG(float &x, float &y, float &z) {
38+
float voltX, voltY, voltZ;
39+
readVoltage(voltX, voltY, voltZ);
40+
41+
x = (voltX - ZERO_VOLTAGE) / SENSITIVITY;
42+
y = (voltY - ZERO_VOLTAGE) / SENSITIVITY;
43+
z = (voltZ - ZERO_VOLTAGE) / SENSITIVITY;
44+
}

src/7semi_ADXL335.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
* @file 7semi_ADXL335.h
3+
* @brief Arduino library for the ADXL335 3-axis analog accelerometer
4+
* @details Reads analog signals from the sensor and provides raw, voltage, and g-force outputs.
5+
* @author 7semi
6+
* @copyright (c) 2025
7+
*/
8+
9+
#ifndef _7SEMI_ADXL335_H_
10+
#define _7SEMI_ADXL335_H_
11+
12+
#include "Arduino.h"
13+
14+
// Constants for ADC conversion
15+
#define REF_VOLTAGE 5 // Reference voltage in volts
16+
#define ZERO_VOLTAGE 1.75 // Voltage at 0g (typically 1.6V for ADXL335)
17+
#define SENSITIVITY 0.33 // Sensitivity in V/g (typically 330mV/g)
18+
19+
/*!
20+
* @class ADXL335_7semi
21+
* @brief Class to interface with the ADXL335 sensor
22+
*/
23+
class ADXL335_7semi {
24+
public:
25+
ADXL335_7semi(uint8_t x_pin = A0, uint8_t y_pin = A1, uint8_t z_pin = A2);
26+
void begin();
27+
void readRaw(int &x, int &y, int &z);
28+
void readVoltage(float &x, float &y, float &z);
29+
void readG(float &x, float &y, float &z);
30+
31+
private:
32+
uint8_t x_pin, y_pin, z_pin;
33+
};
34+
35+
#endif // _7SEMI_ADXL335_H_

0 commit comments

Comments
 (0)