0
votes

So I currently have a pic32 arduino. I'm pretty new to this stuff, so any tips would be appreciated.

I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0.

For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.

If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.

Here is my code:

const int sensor = 0; //sensor port
int sensorState = LOW;

void setup(){ 
pinMode(ledPin, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(sensor,  INPUT);
Serial.begin(9600);
}

void loop(){
sensorState = digitalRead(sensor);
if(sensorState == HIGH)
   alarm();
digitalWrite(ledPin, sensorState);
Serial.println(sensorState);
}
1

1 Answers

1
votes

You may have the internal pull-up resistor enabled, so when nothing is connected, it will read high.

Also, these two statements are contradictory:

For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.

If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.

So the program always reads high but the LED toggles on or off? Which one is it?

If you manually pull the pin to ground, does your program react the way it is supposed to? If it does, then you should take a look at your sensor circuit.

Your sensor circuit sounds weird - you say

I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0

So the sensor output and ground are connected to pin zero? 5v should go to 5v, ground should go to ground, the sensor output should go to pin zero.