The NanoBrick.
A simple Nano project that exhibits the many functionalities of the nano 33 Ble sense.
Devices & Components
1
Arduino Nano 33 BLE Sense Rev2
1
0.96 inch ST7735 SPI oled display.
1
Standard Button
1
Li-Po-Battery 3.7 V 1200 mA
Hardware & Tools
1
Friend with a 3D printer
1
soldering iron
1
Hot Glue Gun
1
Soldering Wire
1
Needle files
Software & Tools
Arduino IDE
Project description
Code
Nano
cpp
Main code.
1#include <Adafruit_GFX.h> // Core graphics library 2#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735 3#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 4#include <SPI.h> 5#include "Arduino_BMI270_BMM150.h" 6#include <Arduino_LPS22HB.h> 7#include <Arduino_HS300x.h> 8#include <Arduino_APDS9960.h> 9#include <PDM.h> 10 11#define TFT_CS 8 12#define TFT_RST 10 // Or set to -1 and connect to Arduino RESET pin 13#define TFT_DC 9 14 15Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); 16 17int counter = 0; 18int buttonPin = 2; 19int buttonState; 20int State = LOW; 21int instances = 11; // The amount of menu logic options. 22float X, Y, Z; 23int R, G, B; 24float proxy = 0; 25 26unsigned long lastDebounceTime = 0; 27unsigned long debounceDelay = 50; 28 29short sampleBuffer[256]; 30volatile int samplesRead; 31 32 33void setup() { 34 // put your setup code here, to run once: 35 tft.initR(INITR_MINI160x80); 36 tft.setRotation(2); 37 tft.setTextWrap(true); 38 tft.fillScreen(ST7735_BLACK); 39 pinMode(buttonPin, INPUT_PULLUP); 40 pinMode(LEDR, OUTPUT); 41 pinMode(LEDG, OUTPUT); 42 pinMode(LEDB, OUTPUT); 43 digitalWrite(LEDR, HIGH); 44 digitalWrite(LEDG, HIGH); 45 digitalWrite(LEDB, HIGH); 46 IMU.begin(); 47 BARO.begin(); 48 HS300x.begin(); 49 APDS.begin(); 50 PDM.onReceive(onPDMdata); 51 PDM.begin(1, 16000); 52} 53void loop() { 54 // put your main code here, to run repeatedly: 55 managebutton(); 56 modeManage(State); 57} 58void managebutton(){ 59 int reading = digitalRead(buttonPin); 60 61 if (reading != State) { 62 lastDebounceTime = millis(); 63 } 64 65 if ((millis() - lastDebounceTime) > debounceDelay) { 66 if (reading != buttonState) { 67 buttonState = reading; 68 69 } 70 } 71 State = reading; 72} 73void modeManage(int status){ 74 if (status == LOW){ 75 if (counter != instances){ 76 counter++; 77 tft.fillScreen(ST7735_BLACK); 78 tft.setTextColor(ST77XX_WHITE); 79 } 80 else { 81 counter = 0; 82 } 83 } 84 85 switch (counter) { 86 case 0: runAccelerometer(); break; 87 case 1: runGyroscope(); break; 88 case 2: runPressure(); break; 89 case 3: runAltitude(); break; 90 case 4: runMagnetometer(); break; 91 case 5: runTemperature(); break; 92 case 6: runHumidity(); break; 93 case 7: runColor(); break; 94 case 8: runProxy(); break; 95 case 9: runMicrophone(); break; 96 case 10: runFlashlight(); break; 97 } 98} 99void runAccelerometer(){ 100 IMU.readAcceleration(X, Y, Z); 101 tft.setCursor(0, 0); 102 tft.setTextColor(ST77XX_WHITE); 103 tft.setTextSize(1); 104 tft.println("Acceleration"); 105 tft.setCursor(0, 50); 106 tft.fillRect(15, 50, 35, 30, ST77XX_BLACK); 107 tft.print("X: "); 108 tft.println(X); 109 analogWrite(LEDR, 255 - axisToPWM(X)); 110 tft.print("Y: "); 111 tft.println(Y); 112 analogWrite(LEDG, 255 - axisToPWM(Y)); 113 tft.print("Z: "); 114 tft.println(Z); 115 analogWrite(LEDB, 255 - axisToPWM(Z)); 116} 117void runGyroscope(){ 118 IMU.readGyroscope(X, Y, Z); 119 tft.setCursor(0, 0); 120 tft.setTextColor(ST77XX_WHITE); 121 tft.setTextSize(1); 122 tft.println("Gyroscope"); 123 tft.setCursor(0, 50); 124 tft.fillRect(15, 50, 50, 30, ST77XX_BLACK); 125 tft.print("X: "); 126 tft.println(X); 127 analogWrite(LEDR, 255 - fabs(X)); 128 tft.print("Y: "); 129 tft.println(Y); 130 analogWrite(LEDG, 255 - fabs(Y)); 131 tft.print("Z: "); 132 tft.println(Z); 133 analogWrite(LEDB, 255 - fabs(Z)); 134} 135void runPressure(){ 136 float P = BARO.readPressure(); 137 tft.setCursor(0, 0); 138 tft.setTextColor(ST77XX_WHITE); 139 tft.setTextSize(1); 140 tft.println("Air Pressure"); 141 tft.setCursor(0, 50); 142 tft.fillRect(15, 50, 45, 10, ST77XX_BLACK); 143 tft.print("P: "); 144 tft.print(P); 145 tft.println(" kPa"); 146 analogWrite(LEDR, 255 - fabs(P)); 147} 148void runAltitude(){ 149 float A = BARO.readPressure(); 150 float altitude = 44330 * ( 1 - pow(A/101.325, 1/5.255) ); 151 float altitudeFeet = altitude * 3.28084; 152 tft.setCursor(0, 0); 153 tft.setTextColor(ST77XX_WHITE); 154 tft.setTextSize(1); 155 tft.println("Altitude"); 156 tft.setCursor(0, 50); 157 tft.fillRect(0, 50, 45, 10, ST77XX_BLACK); 158 tft.print(altitudeFeet); 159 tft.println(" ft"); 160 analogWrite(LEDG, 255 - fabs(altitudeFeet)); 161} 162void runMagnetometer(){ 163 IMU.readMagneticField(X, Y, Z); 164 tft.setCursor(0, 0); 165 tft.setTextColor(ST77XX_WHITE); 166 tft.setTextSize(1); 167 tft.println("Magnetometer"); 168 tft.setCursor(0, 50); 169 tft.fillRect(15, 50, 45, 30, ST77XX_BLACK); 170 tft.print("X: "); 171 tft.println(X); 172 analogWrite(LEDR, 255 - fabs(X)); 173 tft.print("Y: "); 174 tft.println(Y); 175 analogWrite(LEDG, 255 - fabs(Y)); 176 tft.print("Z: "); 177 tft.println(Z); 178 analogWrite(LEDB, 255 - fabs(Z)); 179} 180void runTemperature(){ 181 float CT = HS300x.readTemperature(); 182 float FT = (CT * 1.8) + 32; 183 tft.setCursor(0, 0); 184 tft.setTextColor(ST77XX_WHITE); 185 tft.setTextSize(1); 186 tft.println("Temperature"); 187 tft.setCursor(0, 50); 188 tft.fillRect(0, 50, 45, 30, ST77XX_BLACK); 189 tft.print(FT); 190 tft.print(" "); 191 tft.write(248); 192 tft.println("F"); 193 tft.print(CT); 194 tft.print(" "); 195 tft.write(248); 196 tft.println("C"); 197} 198void runHumidity(){ 199 float H = HS300x.readHumidity(); 200 tft.setCursor(0, 0); 201 tft.setTextColor(ST77XX_WHITE); 202 tft.setTextSize(1); 203 tft.println("Humidity"); 204 tft.setCursor(0, 50); 205 tft.fillRect(0, 50, 40, 30, ST77XX_BLACK); 206 tft.print(H); 207 tft.println(" %"); 208 analogWrite(LEDB, 255 - fabs(H)); 209} 210void runColor(){ 211 if (APDS.colorAvailable()){ 212 APDS.readColor(R, G, B); 213 } 214 analogWrite(LEDR, 255); 215 analogWrite(LEDG, 255); 216 analogWrite(LEDB, 255); 217 tft.setCursor(0, 0); 218 tft.setTextColor(ST77XX_WHITE); 219 tft.setTextSize(1); 220 tft.println("Light"); 221 tft.setCursor(0, 50); 222 tft.fillRect(15, 50, 45, 30, ST77XX_BLACK); 223 tft.print("R: "); 224 tft.println(R); 225 tft.print("G: "); 226 tft.println(G); 227 tft.print("B: "); 228 tft.println(B); 229} 230void runProxy(){ 231 if (APDS.proximityAvailable()) { 232 proxy = APDS.readProximity(); 233 } 234 analogWrite(LEDR, 255); 235 analogWrite(LEDG, 255); 236 analogWrite(LEDB, 255); 237 tft.setCursor(0, 0); 238 tft.setTextColor(ST77XX_WHITE); 239 tft.setTextSize(1); 240 tft.println("Proximity"); 241 tft.setCursor(0, 50); 242 tft.fillRect(35, 50, 45, 20, ST77XX_BLACK); 243 tft.print("Prox: "); 244 tft.println(proxy); 245 tft.setCursor(0, 75); 246 tft.print("Disclaimer: This sensor relies on reflectance. Results will vary with material."); 247} 248void runMicrophone() { 249 if (samplesRead > 0) { 250 long sum = 0; 251 for (int i = 0; i < samplesRead; i++) { 252 sum += abs(sampleBuffer[i]); 253 } 254 int volume = sum / samplesRead; // average amplitude 255 int ledValue = map(volume, 0, 2000, 0, 255); 256 ledValue = constrain(ledValue, 0, 255); 257 258 analogWrite(LEDR, 255 - ledValue); 259 analogWrite(LEDG, 255 - ledValue); 260 analogWrite(LEDB, 255 - ledValue); 261 tft.setCursor(0, 0); 262 tft.setTextColor(ST77XX_WHITE); 263 tft.setTextSize(1); 264 tft.println("Microphone"); 265 tft.setCursor(0, 50); 266 tft.fillRect(25, 50, 45, 20, ST77XX_BLACK); 267 tft.print("Mic: "); 268 tft.println(volume); 269 270 samplesRead = 0; // reset so next buffer can be processed 271 } 272} 273void runFlashlight() { 274 tft.fillScreen(ST77XX_WHITE); 275 analogWrite(LEDR, 0); 276 analogWrite(LEDG, 0); 277 analogWrite(LEDB, 0); 278} 279int axisToPWM(float v) { 280 v = fabs(v); // absolute acceleration 281 v = constrain(v, 0, 2); // typical range when tilting 282 return map(v * 100, 0, 200, 0, 255); 283} 284void onPDMdata() { 285 // query the number of bytes available 286 int bytesAvailable = PDM.available(); 287 288 // read into the sample buffer 289 PDM.read(sampleBuffer, bytesAvailable); 290 291 // 16-bit, 2 bytes per sample 292 samplesRead = bytesAvailable / 2; 293}
Downloadable files
Nano
Case for the system.
Nano.stl
Comments
Only logged in users can leave comments