Hello Arduino world!
At the end of this tutorial you will be able to use a piezo element to activate lights on an LED strip.
Before we begin, let’s better understand what is occurring with piezoelectric sensors. Piezoelectric sensors utilize the idea that everything gives off vibrations and shocks when it moves, and therefore, this vibration can be sensed and harvested to power other elements.
This tutorial focuses on the larger idea that vibrations from trains could be used to power installations around cities, if the installation is close enough to the source. Some of these installations may use LED lights or strips as indicators, which is where this tutorial comes in.
For now, we’ll just look at a small portion of this idea – how readings from the piezo element can control the lights on the LED strip.
Let’s begin!
Materials Needed:
- Arduino Uno board
- Breadboard
- 0M ohm resistor
- Piezo Element
- 10 Segment LED Bar Graph
- 10 330-ohm resistors
- Alligator clips
- Jumper wires
- Aluminum foil
- Toy train
Wiring the Board:
Before creating the circuit, it is important to understand how much electrical current is needed for each part to operate and how much resistance is required. The piezo element requires a 1.0M ohm resistor compared to the LED lights which require much less, 330 ohms. In fact, the 330-ohm resistors provide more resistance than necessary for the LED strip, but it is always safer to use a higher ohm resistor than what is required rather than a lower ohm resistor if that is what you have on hand. If you would like to learn more about how to calculate the necessary resistance, here is a helpful resource.
The most important pin connection in this circuit is connecting the piezo element’s red wire to pin A0. This is an analog pin, which allows the information from the piezo element to be sent to the computer. The breadboard diagram below will help you make sure that connection is being made, along with setting up the LED strips. Because the LED strip is composed of 10 individual lights, each one needs to be connected to a pin and a resistor is used to ground each one.
You may notice that the piezo element’s wires are much thinner and shorter than the other jumper wires. While you can connect the piezo directly to the breadboard with those wires, I found that they were too short for it to lay flat on the table. To get around this, I made a connection between leading jumper wires and the piezo with alligator clips.
In the diagram above, the jumper wire is connecting pin A0 to breadboard location D5. I then put in another wire in E5 that leads to the piezo element’s red wire. If you try to just connect the piezo element’s wire to the jumper wire with the alligator clips, the piezo element’s wire will fall out because it is too thin. Wrap the two ends in aluminum foil and use the alligator clip to hold the connection in place. Repeat this with a wire leading from the piezo element’s black wire to the ground on the breadboard.
After you wire your breadboard, I suggest tracing the circuit with your finger to double check all the wiring is done correctly before moving on to the next steps.
The Code:
The code below walks through how to send data from the piezo element to your computer. By using the serial monitor window, appropriate thresholds can be set to turn on and off segments of the LED strip.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
/* Piezo Element and LED Lights This sketch will how you how to read sensor information from a piezo element and use that data to control an strip of 10 LEDs. Elements of this sketch include global variables and if/else statements */ //set global variables const int piezoSensor = A0; //the analog pin the piezo element is connected to int sensorReading = 0; //variable to store the value read from the sensor //each LED in the strip acts as an individual light //therefore, each light must be connected to its own pin on the arduino //and a variable must be set for each int Pin1 = 4; int Pin2 = 5; int Pin3 = 6; int Pin4 = 7; int Pin5 = 8; int Pin6 = 9; int Pin7 = 10; int Pin8 = 11; int Pin9 = 12; int Pin10 = 13; //set up the elements of this sketch void setup() { //because the pins are only reacting to the sensor, they are set as outputs pinMode(Pin1, OUTPUT); pinMode(Pin2, OUTPUT); pinMode(Pin3, OUTPUT); pinMode(Pin4, OUTPUT); pinMode(Pin5, OUTPUT); pinMode(Pin6, OUTPUT); pinMode(Pin7, OUTPUT); pinMode(Pin8, OUTPUT); pinMode(Pin9, OUTPUT); pinMode(Pin10, OUTPUT); Serial.begin(9600); //start the serial monitor window //this will help you see the values coming from the piezo element to //set the appropriate thresholds in the loop below } void loop() { //store the readings from the piezo element as "sensorReading" sensorReading = analogRead(piezoSensor); //open the serial monitor window to determine the appropriate ranges of values //Note: the baud rate of the window must match the baud rate in the Serial.begin code above Serial.println(sensorReading); //use an if/else statement to control the light strip if (sensorReading > 0 && sensorReading <= 100) //if sensor value is greater than 0, but less than/equal to 100 { digitalWrite(Pin1, HIGH); //turn on the first three lights digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); digitalWrite(Pin4, LOW); //keep all others off digitalWrite(Pin5, LOW); digitalWrite(Pin6, LOW); digitalWrite(Pin7, LOW); digitalWrite(Pin8, LOW); digitalWrite(Pin9, LOW); digitalWrite(Pin10, LOW); delay(10); //delay by 10 milliseconds } else if (sensorReading > 100 && sensorReading <= 300) //if value is greater than 100, but less than/equal to 300 { digitalWrite(Pin1, HIGH); //turn on the first 7 lights digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); digitalWrite(Pin4, HIGH); digitalWrite(Pin5, HIGH); digitalWrite(Pin6, HIGH); digitalWrite(Pin7, HIGH); digitalWrite(Pin8, LOW); //keep all others off digitalWrite(Pin9, LOW); digitalWrite(Pin10, LOW); delay(10); //delay by 10 milliseconds } else if (sensorReading > 300) //if value is greater than 300 { digitalWrite(Pin1, HIGH); //turn on all the lights digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); digitalWrite(Pin4, HIGH); digitalWrite(Pin5, HIGH); digitalWrite(Pin6, HIGH); digitalWrite(Pin7, HIGH); digitalWrite(Pin8, HIGH); digitalWrite(Pin9, HIGH); digitalWrite(Pin10, HIGH); delay(10); //delay by 10 milliseconds } else //if the sensor is reading 0 -- no vibrations { digitalWrite(Pin1, LOW); //have all the lights be off digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); digitalWrite(Pin4, LOW); digitalWrite(Pin5, LOW); digitalWrite(Pin6, LOW); digitalWrite(Pin7, LOW); digitalWrite(Pin8, LOW); digitalWrite(Pin9, LOW); digitalWrite(Pin10, LOW); delay(10); //delay by 10 milliseconds } } |
Copy and paste this code into a blank sketch and upload to your board. While I use a train to simulate vibrations and shocks, you can test whether the piezo element is working just by tapping on the piezo element pad. Enjoy!