Ultrasonic Sensor Controlled 8x8 LED Display Matrix by William and Charlie
by PHS_Engineering in Circuits > Arduino
1597 Views, 1 Favorites, 0 Comments
Ultrasonic Sensor Controlled 8x8 LED Display Matrix by William and Charlie
In this basic project, you will control an 8x8 LED Matrix using an ultrasonic sensor. The distance of your hand will determine the row of LED lights that light up on the matrix. Although this is a basic project, there is lots of potential to turn it into other cool and complex projects such as a dino-run like game.
This project requires the LedControl library. Click HERE to download it.
Supplies
This project will require:
-Arduino uno
-breadboard
-ultrasonic sensor
-8x8 LED matrix
-jumper wires
-Male to Female wires
-USB cord
-Card Board box (optional)
Connect the 8x8 LED Matrix
This the device which features the LEDs. There are five wires in total, coming off of the five prongs on the left side of the matrix. First, connect the 5V pin to the VCC on the matrix. Next, connect the ground pin (GND) to the ground pin (GND) on the matrix. Finally, connect the DIN, CS, and CLK pins to digital pins on the Arduino. For our code we connected the DIN pin to 12, the CS pin to 10, and the CLK pin to 11 on the Arduino. For more help see the circuit diagram at the bottom of this post.
Connect the UltraSonic Sensor
In this step, you will connect the ultrasonic sensor to the breadboard. This Arduino device allows the user to determine where the LED row lies on the Matrix based on movement. The farther away your hand is, the higher the dot will read on the matrix, the closer your hand is to the center, the lower the dot is in relation to the matrix. To hook this up, connect the GND pin to the ground, the VCC pin to 5V, the Trig pin to 3, and the Echo pin to 2. For more help see the circuit diagram at the bottom of this post.
Write the Code
Below is the required code for this project to come to fruition:
#include "LedControl.h"
#include "binary.h"
/* DIN connects to pin 12 CLK connects to pin 11 CS connects to pin 10 */
LedControl lc=LedControl(12,11,10,1);
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 // defines variables
int duration; // variable for the duration of sound wave travel
int distance;//variable for the distance measurement
int dm; // variable for what row to light up on the matrix
// Binary for the bar
byte dot[8]= {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111};
void setup() {
lc.shutdown(0,false); //sets up 8x8 matrix
lc.setIntensity(0,100000); // Set brightness to a medium value
lc.clearDisplay(0); // Clear the display
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //Creates a ten microsecond delay
digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
//The following if statements convert the distance from the ultrasonic sensor into a variable for the matrix called "dm"
if(distance<=3 and distance>=0){
dm=7;
}
//This section of code lights up the LEDs on the matrix
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=5 and distance>=4){
dm=6;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=7 and distance>=6){
dm=5;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=9 and distance>=8){
dm=4;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=11 and distance>=10){
dm=3;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=13 and distance>=12){
dm=2;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=15 and distance>=14){
dm=1;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=19 and distance>=16){
dm=0;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
}
Circuit
Here is a circuit diagram to help wire up the project.
Upload Code and Enjoy!
Congratulations! You have just completed the project. If you would like, cut out a cardboard box to put the board in like we did to hide all the wires. Add a comment if you ended up modifying our code to make other cool projects! Also be sure to check out other projects our peers have created at Portland High School under the PHS_Engineering account!