• No products in the cart.

LED dimming with pressure sensor

  /  Make   /  LED   /  LED dimming with pressure sensor

How to develop a pressure depending LED dimming

In this tutorial we will show you how to dim LEDs depending on the pressure measured with the pressure sensor textile.

First of all we connect both textiles to the board and set the jumper P4 to the correct position (pressure):

Afterwards we can test the pressure sensor using the serial monitor of the arduino IDE:

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
}

After downloading this program to the board we open the serial monitor and watch the values that depends on the pressure on the sensor:

Now we can test the LEDs by simply switching them on:

#define LED1_PIN 10
#define LED2_PIN 11
void setup() {
Serial.begin(9600);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
}
void loop() {
Serial.println(analogRead(A0));
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
}

Again we download the program to the board and we should see the LEDs shining.

As we have tested all peripheral elements we can now code the final function. Therefore we read the analog value of the pressure sensor which varies between 0 and 1023. The dimming of the LEDs can be easily done with PWM which is used by calling the “analogWrite” methode in the Arduino IDE. The first parameter defines the pin and the second must be a value between 0 and 255. 0 means completely off and 255 keeps the pin HIGH all the time. As the maximum value of the pressure sensor is 1023 we have to divide it by 4 to get a maximum value of 255.
#define LED1_PIN 10
#define LED2_PIN 11
void setup() {
// put your setupcode here, to run once:
Serial.begin(9600);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int pressValue = analogRead(A0);
if(pressValue > 260) // threshold
{
analogWrite(LED1_PIN, (pressValue - 260) / 3);
// read analog value: 0...1023, write analog value: 0...255 -> divide by 3
analogWrite(LED2_PIN, (pressValue - 260) / 3);
}
else
{
analogWrite(LED1_PIN, 0);
analogWrite(LED2_PIN, 0);
}
Serial.println(analogRead(A0));
}

Finally we download this code again to the board.
Try it out and have fun!