Im trying to make a Low pressure light indicator using a 30psi pressure sensor with 3 wires (green,red, black). whenever the pressure drops below the condition, an LED will lit. I need help with the coding Im kinda new to arduino.
Pressure sensor 3wires:
Red wire(+5)
Black (GND)
GREEN (A0)
LED Wiring:
LED Cathod to ground, Anode through a 220 Ohms resistor to D13
Heres my code so far.
const int sensorPin = A0; // Analog pin connected to the sensor's signal
const int ledPin = 13; // Digital pin connected to the LED
// Set your low-pressure threshold in PSI (e.g., 10 PSI)
const float lowPressureThreshold = 10.0;
// ADC resolution and sensor specifications
const int adcMax = 1023; // 10-bit ADC
const float sensorMaxVoltage = 5.0; // Sensor output range 0-5V
const float sensorMaxPressure = 30.0; // Sensor max pressure in PSI
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
float voltage = (sensorValue / float(adcMax)) * sensorMaxVoltage;
float pressure = (voltage / sensorMaxVoltage) * sensorMaxPressure;
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" PSI");
if (pressure < lowPressureThreshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(1000); // Read every second
}
What is 'the condition' and how is it determined? Is it something that will likely change during use, or can/should it be hardwired into the system?
What's the code you have so far, and how have you wired your system - i.e. can you post a schematic, please? Also a photograph of the setup that shows how everything is connected.
Welcome! We have a problem, we want to help but cannot see what you have. Your picture is nice but I have seen pressure switches and sensors in the same package. Without knowing exactly what you have we cannot answer your question accurately. The schematic should only take maybe 1/2 hour depending on what background you have.
If you have a voltmeter, you can measure the voltage between the blue (green?) wire and the black wire from the sensor and see if it is fluctuating or steady. That could tell you if the problem is the sensor or something to do with the Arduino. But yeah, we would really like to see a datasheet on the sensor. I don't need to see a schematic, because I can infer one from your photos.
Less a link to your sensor it's hard to tell what is what. Your code is written around a sensor out of 0.0 to 5.0 volts, you are absolutely sure that is what you have? Here is a sensor like yours and many sensors like this output 0.5 to 4.5 volts.
Next, as suggested, measure the DC voltage out of the sensor to yourA0 pin of your Arduino. Is it stable? Yes or no?
The first thing to do when developing such a device is to take it in parts:
First part should be to start with the example file for analog read (file/examples/analog....
However change the code to print the raw analog input value to the serial monitor. Then see what is happening. Be sure to triple check your wiring as a varying analog input value can be caused by an open circuit.
And that's what I was getting at. The code, wherever it came from, is for a 0.0 to 5.0 volt out of the sensor. There is a Vout span of 4.0 volts which begins with 0.5 volts and ends with 4.5 volts so fix your code accordingly if you have this sensor.
But, you have to keep the knowledge level of the customer in mind, I've seen people try to replace resistive sensors with these and try to test them with an Ohmmeter, the results of which are useless.