0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, let me clarify. The program always reads high, but if instead of connecting the output back to the board, I connect it to an LED, the LED will toggle on and off. For the second question: I have the 5VDC connected to 5VDC, ground to ground, and the output to PORT0. The reason I had it wired kind of weird is because each port has 2 pins, while I only have one output wire. So I tried connected ground to the second pin at PORT0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.