Why isn't my LED responding to pressure readings in AVR128DA48 project?

hey guys, In my ongoing project on real-time pressure monitoring using the AVR128DA48 microcontroller, I’ve set up an alert system that triggers an LED to blink when the pressure exceeds a certain threshold. However, the LED doesn't seem to respond correctly to the pressure values, I have tested the LED separately to ensure it is functioning, verified that the BMP280 sensor is returning valid pressure readings, and also checked the GPIO pin configuration for the LED, yet still fails to respond correctly to the pressure readings, even when the threshold is crossed. this is my code below:
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>

#define LED_PIN 13
#define PRESSURE_THRESHOLD 1000 // in kPa

void main(void)
{
const struct device *led_dev = device_get_binding("GPIO_0");
if (led_dev == NULL) {
printk("Failed to bind to LED GPIO device\n");
return;
}
gpio_pin_configure(led_dev, LED_PIN, GPIO_OUTPUT);

const struct device *bmp280 = device_get_binding(DT_LABEL(DT_INST(0, bosch_bmp280)));
struct sensor_value pressure;

while (1) {
sensor_sample_fetch(bmp280);
sensor_channel_get(bmp280, SENSOR_CHAN_PRESS, &pressure);

if (pressure.val1 > PRESSURE_THRESHOLD) {
gpio_pin_set(led_dev, LED_PIN, 1); // Turn on LED
} else {
gpio_pin_set(led_dev, LED_PIN, 0); // Turn off LED
}
k_sleep(K_SECONDS(1));
}
}
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>

#define LED_PIN 13
#define PRESSURE_THRESHOLD 1000 // in kPa

void main(void)
{
const struct device *led_dev = device_get_binding("GPIO_0");
if (led_dev == NULL) {
printk("Failed to bind to LED GPIO device\n");
return;
}
gpio_pin_configure(led_dev, LED_PIN, GPIO_OUTPUT);

const struct device *bmp280 = device_get_binding(DT_LABEL(DT_INST(0, bosch_bmp280)));
struct sensor_value pressure;

while (1) {
sensor_sample_fetch(bmp280);
sensor_channel_get(bmp280, SENSOR_CHAN_PRESS, &pressure);

if (pressure.val1 > PRESSURE_THRESHOLD) {
gpio_pin_set(led_dev, LED_PIN, 1); // Turn on LED
} else {
gpio_pin_set(led_dev, LED_PIN, 0); // Turn off LED
}
k_sleep(K_SECONDS(1));
}
}
What might be causing this issue, and how can I ensure the LED correctly responds to pressure values?
Solution:
Yea thankz @Marvee Amasi I checked the issue was coming from my comparison of the pressure values...I had to use a single unit for it, both for the integer and fractional values..thanks for pointing out
Jump to solution
4 Replies
Marvee Amasi
Marvee Amasi4mo ago
Hi @Dtynin your PRESSURE_THRESHOLD is defined in kPa, but ensure the pressure.val1 value is also in the same kPa. If not, you'll need to convert the units accordingly tho
Marvee Amasi
Marvee Amasi4mo ago
That your pressure sensor could even be providing noisy readings, that is causing pressure values to fluctuate around the threshold, preventing the LED from staying consistently on. Try filtering the pressure readings using moving average or low pass filter
Marvee Amasi
Marvee Amasi4mo ago
k_sleep(K_SECONDS(1)) might be too long. If the pressure changes rapidly, the LED might not have enough time to respond. Try reducing the sleep time too
Solution
Dtynin
Dtynin4mo ago
Yea thankz @Marvee Amasi I checked the issue was coming from my comparison of the pressure values...I had to use a single unit for it, both for the integer and fractional values..thanks for pointing out
Want results from more Discord servers?
Add your server