Using Analog Pin
-
I want to connect liquid level detection sensor that is an analog signal. The sensor gives (0-5)V voltage output according to water level. But spresense output is very random.
int val=0;
int pin=A0;void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}void loop() {
// put your main code here, to run repeatedly:
val=analogRead(pin);Serial.print("Water level: "); int level=0.304063*val-162.64; if(level<0){ level=0; } Serial.println(level,"mm"); delay(1000);
}
This is my code.
-
Hey, @Ajit01
In the past when I got random results from analog signals I had forgotten to connect the sensor GND with Spresense GND. Please check this.
But to make sure that everything is good with the spresense analog in, you can test connecting the 3.3V pin to A0 and then the 5V pin to A0 and printing the value.
Doing this here I got 658 with 3.3V and 986 with 5V.
Code I used to test mine:
int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor void setup() { Serial.begin(9600); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue); delay(1000); }