Components and supplies
SparkFun Triple Axis Accelerometer Breakout - ADXL345
0.1 inch connector socket 15 way
Arduino Nano R3
Stripboard 37*24 holes
MAXX7219 Display unit
0.1 inch connector socket 8 way
0.1 inch connector plug 5 way
4K7 resistor
Apps and platforms
Arduino IDE
Project description
Code
Screen rotation code
c_cpp
1/* 2 * December 2021 3 * 4 * Use adxl345 to rotate display through portrait and landscape modes depending on sensor orientation. 5 * 6 * Sketch is based on Example -> Adafruit ADXL345 -> sensortest.ino 7 * 8 * Orientation code source: 9 * Datasheet AN3461 - 6 Selecting portrait and landscape modes 10 * https://www.nxp.com/docs/en/application-note/AN3461.pdf 11 * (checked Dec 2021) 12 * 13 */ 14 15// Circuit: 16// 17// ADXL345 pinout 18// Gnd to Gnd 19// Vcc to +5v 20// SDA to Nano pin A5 21// SCL to Nano pin A4 22// 23// Resistors 24// 4K7 pullup resistor one end to Vcc the other to Nano pin A4 25// 4K7 pullup resistor one end to Vcc the other to Nano pin A5 26// 27// MAXX7219 pinout 28// Vcc to +5v 29// Gnd to Gnd 30// Din to Nano pin 10 31// CS to Nano pin 11 32// CLK to Nano pin 13 33 34 35// Additional libraries used 36// Adafruit Unified Sensor 37// Adafruit ADXL345 38// These can be installed via Sketch -> Include Libraries -> Manage Libraries... (you will need to be online) 39// Installing Adafruit ADXL345 first will prompt for Adafruit Unified Sensor to be installed - you need both 40 41#include <Wire.h> 42#include <Adafruit_Sensor.h> 43#include <Adafruit_ADXL345_U.h> 44 45#include <LedControl.h> 46/* 47 Now we need a LedControl to work with. 48 ***** These pin numbers will probably not work with your hardware ***** 49 pin xx is connected to the DataIn 50 pin xx is connected to the CLK 51 pin xx is connected to LOAD 52 We have only a single MAX72XX. 53 */ 54// Matrix 55#define PIN_DATAIN 11 56#define PIN_CLK 13 57#define PIN_LOAD 10 58 59LedControl lc = LedControl(PIN_DATAIN, PIN_CLK, PIN_LOAD, 1); 60 61/* Assign a unique ID to this sensor at the same time */ 62Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); 63 64#define zero_x 1.569 65 66#define zero_y 1.569 67 68#define zero_z 1.569 69 70void setup(void) 71{ 72#ifndef ESP8266 73 while (!Serial); // for Leonardo/Micro/Zero 74#endif 75 Serial.begin(9600); 76 Serial.println("Accelerometer Test"); Serial.println(""); 77 78 /* Initialise the sensor */ 79 if(!accel.begin()) 80 { 81 /* There was a problem detecting the ADXL345 ... check your connections */ 82 Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); 83 while(1); 84 } 85 86 /* Set the range to whatever is appropriate for your project */ 87 //accel.setRange(ADXL345_RANGE_16_G); 88 // accel.setRange(ADXL345_RANGE_8_G); 89 // accel.setRange(ADXL345_RANGE_4_G); 90 accel.setRange(ADXL345_RANGE_2_G); 91 92 /* 93 The MAX72XX is in power-saving mode on startup, 94 we have to do a wakeup call 95 */ 96 lc.shutdown(0,false); 97 /* Set the brightness to a medium values */ 98 lc.setIntensity(0,8); 99 /* and clear the display */ 100 lc.clearDisplay(0); 101 102} 103 104void loop(void) 105{ 106 /* Get a new sensor event */ 107 sensors_event_t event; 108 accel.getEvent(&event); 109 110 111 112float xv; 113float yv; 114float zv; 115 116xv=(event.acceleration.x-zero_x); 117yv=(event.acceleration.y-zero_y); 118zv=(event.acceleration.z-zero_z); 119 120 121/* 122 * The orientation code 123 * 124 (|G pz | < 0.5g) AND (G px > 0.5g) AND (|G py | < 0.4g): Change orientation to Top 125 (|G pz | < 0.5g) AND (G px < -0.5g) AND (|G py | < 0.4g): Change orientation to Bottom 126 (|G pz | < 0.5g) AND (G py > 0.5g) AND (|G px | < 0.4g): Change orientation to Right 127 (|G pz | < 0.5g) AND (G py < -0.5g) AND (|G px | < 0.4g): Change orientation to Left. 128|x| means the absoulute value of x 129ADXL345 sensor gives results in ms^2 but thr formula above requires result in g force so you need to convert the g values 1300.4g=3.9ms^2 1310.5g=4.9ms^2 132Conversion source online conversion calculator: https://www.convertunits.com/from/g-unit/to/m/(s%5e2) 133 134*/ 135 136 137if ((abs(zv) < 4.9) && (xv > 4.9) && (abs(yv) < 3.9)) {rotate_display('t');} //Change orientation to Top 138if ((abs(zv) < 4.9) && (xv < -4.9) && (abs(yv) < 3.9)) {rotate_display('b');} //Change orientation to Bottom 139if ((abs(zv) < 4.9) && (yv > 4.9) && (abs(xv) < 3.9)) {rotate_display('r');} //Change orientation to Right 140if ((abs(zv) < 4.9) && (yv < -4.9) && (abs(xv) < 3.9)) {rotate_display('l');} //Change orientation to Left. 141 142 143delay(1000); 144 145}// Loop end 146 147// My functions 148void rotate_display(char my_direction) 149{ 150 lc.clearDisplay(0); 151switch(my_direction){ 152 case 't':lc.setLed(0,7,4,true);lc.setLed(0,6,3,true);lc.setLed(0,6,5,true);break;// 153 case 'b':lc.setLed(0,0,4,true);lc.setLed(0,1,3,true);lc.setLed(0,1,5,true);break;// 154 case 'r':lc.setLed(0,4,7,true);lc.setLed(0,3,6,true);lc.setLed(0,5,6,true);break;/// 155 case 'l':lc.setLed(0,4,0,true);lc.setLed(0,3,1,true);lc.setLed(0,5,1,true);break;// 156 } 157 158 } 159
Screen rotation code
c_cpp
1/* 2 * December 2021 3 * 4 * Use adxl345 to rotate display through portrait and landscape modes depending on sensor orientation. 5 * 6 * Sketch is based on Example -> Adafruit ADXL345 -> sensortest.ino 7 * 8 * Orientation code source: 9 * Datasheet AN3461 - 6 Selecting portrait and landscape modes 10 * https://www.nxp.com/docs/en/application-note/AN3461.pdf 11 * (checked Dec 2021) 12 * 13 */ 14 15// Circuit: 16// 17// ADXL345 pinout 18// Gnd to Gnd 19// Vcc to +5v 20// SDA to Nano pin A5 21// SCL to Nano pin A4 22// 23// Resistors 24// 4K7 pullup resistor one end to Vcc the other to Nano pin A4 25// 4K7 pullup resistor one end to Vcc the other to Nano pin A5 26// 27// MAXX7219 pinout 28// Vcc to +5v 29// Gnd to Gnd 30// Din to Nano pin 10 31// CS to Nano pin 11 32// CLK to Nano pin 13 33 34 35// Additional libraries used 36// Adafruit Unified Sensor 37// Adafruit ADXL345 38// These can be installed via Sketch -> Include Libraries -> Manage Libraries... (you will need to be online) 39// Installing Adafruit ADXL345 first will prompt for Adafruit Unified Sensor to be installed - you need both 40 41#include <Wire.h> 42#include <Adafruit_Sensor.h> 43#include <Adafruit_ADXL345_U.h> 44 45#include <LedControl.h> 46/* 47 Now we need a LedControl to work with. 48 ***** These pin numbers will probably not work with your hardware ***** 49 pin xx is connected to the DataIn 50 pin xx is connected to the CLK 51 pin xx is connected to LOAD 52 We have only a single MAX72XX. 53 */ 54// Matrix 55#define PIN_DATAIN 11 56#define PIN_CLK 13 57#define PIN_LOAD 10 58 59LedControl lc = LedControl(PIN_DATAIN, PIN_CLK, PIN_LOAD, 1); 60 61/* Assign a unique ID to this sensor at the same time */ 62Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); 63 64#define zero_x 1.569 65 66#define zero_y 1.569 67 68#define zero_z 1.569 69 70void setup(void) 71{ 72#ifndef ESP8266 73 while (!Serial); // for Leonardo/Micro/Zero 74#endif 75 Serial.begin(9600); 76 Serial.println("Accelerometer Test"); Serial.println(""); 77 78 /* Initialise the sensor */ 79 if(!accel.begin()) 80 { 81 /* There was a problem detecting the ADXL345 ... check your connections */ 82 Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); 83 while(1); 84 } 85 86 /* Set the range to whatever is appropriate for your project */ 87 //accel.setRange(ADXL345_RANGE_16_G); 88 // accel.setRange(ADXL345_RANGE_8_G); 89 // accel.setRange(ADXL345_RANGE_4_G); 90 accel.setRange(ADXL345_RANGE_2_G); 91 92 /* 93 The MAX72XX is in power-saving mode on startup, 94 we have to do a wakeup call 95 */ 96 lc.shutdown(0,false); 97 /* Set the brightness to a medium values */ 98 lc.setIntensity(0,8); 99 /* and clear the display */ 100 lc.clearDisplay(0); 101 102} 103 104void loop(void) 105{ 106 /* Get a new sensor event */ 107 sensors_event_t event; 108 accel.getEvent(&event); 109 110 111 112float xv; 113float yv; 114float zv; 115 116xv=(event.acceleration.x-zero_x); 117yv=(event.acceleration.y-zero_y); 118zv=(event.acceleration.z-zero_z); 119 120 121/* 122 * The orientation code 123 * 124 (|G pz | < 0.5g) AND (G px > 0.5g) AND (|G py | < 0.4g): Change orientation to Top 125 (|G pz | < 0.5g) AND (G px < -0.5g) AND (|G py | < 0.4g): Change orientation to Bottom 126 (|G pz | < 0.5g) AND (G py > 0.5g) AND (|G px | < 0.4g): Change orientation to Right 127 (|G pz | < 0.5g) AND (G py < -0.5g) AND (|G px | < 0.4g): Change orientation to Left. 128|x| means the absoulute value of x 129ADXL345 sensor gives results in ms^2 but thr formula above requires result in g force so you need to convert the g values 1300.4g=3.9ms^2 1310.5g=4.9ms^2 132Conversion source online conversion calculator: https://www.convertunits.com/from/g-unit/to/m/(s%5e2) 133 134*/ 135 136 137if ((abs(zv) < 4.9) && (xv > 4.9) && (abs(yv) < 3.9)) {rotate_display('t');} //Change orientation to Top 138if ((abs(zv) < 4.9) && (xv < -4.9) && (abs(yv) < 3.9)) {rotate_display('b');} //Change orientation to Bottom 139if ((abs(zv) < 4.9) && (yv > 4.9) && (abs(xv) < 3.9)) {rotate_display('r');} //Change orientation to Right 140if ((abs(zv) < 4.9) && (yv < -4.9) && (abs(xv) < 3.9)) {rotate_display('l');} //Change orientation to Left. 141 142 143delay(1000); 144 145}// Loop end 146 147// My functions 148void rotate_display(char my_direction) 149{ 150 lc.clearDisplay(0); 151switch(my_direction){ 152 case 't':lc.setLed(0,7,4,true);lc.setLed(0,6,3,true);lc.setLed(0,6,5,true);break;// 153 case 'b':lc.setLed(0,0,4,true);lc.setLed(0,1,3,true);lc.setLed(0,1,5,true);break;// 154 case 'r':lc.setLed(0,4,7,true);lc.setLed(0,3,6,true);lc.setLed(0,5,6,true);break;/// 155 case 'l':lc.setLed(0,4,0,true);lc.setLed(0,3,1,true);lc.setLed(0,5,1,true);break;// 156 } 157 158 } 159
Downloadable files
Circuit board for Nano, accelerometer & display
Circuit board for Nano, accelerometer & display
Comments
Only logged in users can leave comments