Accelerometer Die

by 786969 in Circuits > Sensors

5 Views, 0 Favorites, 0 Comments

Accelerometer Die

Thumbnail.jpg

Random number from 1-6 when shaken.

Supplies

330 Resistor, breadboard (2), accelerometer micro bit, 7 segment display, Arduino.

Gather Components

Gather the base components, 330 Resistor, breadboard (2), accelerometer micro bit.

Wire Components

content.jpg

Wire all the base components to the breadboard and Arduino.

Code Micro Bit

content.jpg

Code the micro bit in the micro bit's app through a Bluetooth or wired connection.

Attach the 7 Segment Display.

content.jpg

Attach the 7 segment display and wire the segment A to Ard. pin 2, seg B to Ard. pin 3, C to 4, D to 5 , E to 6, F to 7, G to 8.

Final Step- Code the Arduino.

Screenshot 2025-06-17 142352.png

Put the following sketch into Arduino IDE:

#include <SoftwareSerial.h>


// Segment pins: A-G

int segPins[7] = {2, 3, 4, 5, 6, 7, 8};


// Segment ON/OFF map for digits 1–6

const byte digits[6][7] = {

{1, 0, 0, 1, 1, 1, 1}, // 1

{0, 0, 1, 0, 0, 1, 0}, // 2

{0, 0, 0, 0, 1, 1, 0}, // 3

{1, 0, 0, 1, 1, 0, 0}, // 4

{0, 1, 0, 0, 1, 0, 0}, // 5

{0, 1, 0, 0, 0, 0, 0} // 6

};


void setup() {

Serial.begin(9600); // For debugging

// Set all segment pins as output

for (int i = 0; i < 7; i++) {

pinMode(segPins[i], OUTPUT);

digitalWrite(segPins[i], LOW);

}


// Seed random for dice rolls

randomSeed(analogRead(0));

}


void loop() {

if (Serial.available()) {

String input = Serial.readStringUntil('\n');

input.trim(); // Remove newline or spaces


if (input == "SHAKE") {

int roll = random(1, 7); // Get number 1–6

Serial.print("You rolled: ");

Serial.println(roll);

showDigit(roll);

}

}

}


void showDigit(int num) {

if (num < 1 || num > 6) return;


for (int i = 0; i < 7; i++) {

digitalWrite(segPins[i], digits[num - 1][i] ? HIGH : LOW);

}

}


Downloads