My 12 year old son is trying to wire a circuit that can be mounted on a remote control car to detect “potholes” for a STEM project. Ideally we would like continuous output that he can decided what vibration correlates with a pothole detection.
We have an Arduino UNO board and a DFR0027 sensor and are using the following code but cannot get any output from the sensor?? Could anyone tell us where we are going wrong?
Is there a better sensor that we could get an analog output from?
#define SensorLED 13 //Connect the sensor to digital Pin 3 which is Interrupts 1.
#define SensorINPUT 3
unsigned char state = 0;
void setup() {
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
//Trigger the blink function when the falling edge is detected
attachInterrupt(1, blink, FALLING);
}
void loop() {
if (state != 0) {
state = 0;
digitalWrite(SensorLED, HIGH);
delay(500);
}
else
digitalWrite(SensorLED, LOW);
}
//Interrupts function
void blink() {
state++;
}
Hi Natalie
Added to what Jeff said you can’t connect a LED between an output and ground without a current limiting resistor in series.
If this pin happened to be activated you might have a dead LED.
About 330Ω would do but there is plenty of tutorial material on operating LEDs. Research is part of the learning curve. At least I think you have it connected the right way around. It looks like a flat bit pointing to pin 14.
Cheers Bob
As noted above, the indicating LED should have a resistor. However it would be better to simply remove it - Pin 13 is already connected to the on-board LED via a suitable resistor.
You can also look at adding in a print statement to the code, this will give you feedback in the Arduino IDE, I’ve updated your code below to have it
#define SensorLED 13 //Connect the sensor to digital Pin 3 which is Interrupts 1.
#define SensorINPUT 3
volatile byte state = 0;
void setup() {
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
//Trigger the blink function when the falling edge is detected
attachInterrupt(1, blink, FALLING);
}
void loop() {
if (state != 0) {
state = 0;
digitalWrite(SensorLED, HIGH);
Serial.println("Sensor input is HIGH");
delay(500);
}
else
digitalWrite(SensorLED, LOW);
}
//Interrupts function
void blink() {
state++;
}
As you have the same code as the DFRobot example it should work. No need to change it.
Remove the Yellow LED you have plugged in, you don’t need it. There is an onboard LED attached to pin 13 (see pic). The LED you plugged in would have been loading down pin 13.
You cannot connect the sensor to the ICSP port. The 6 pins each have a very specific function.
The sensor should be connected as per the DFRobot web page. (see below)
Debouncing: If the sensor is too sensitive and triggers multiple times for a single vibration, consider adding debouncing logic to ignore multiple triggers within a short time frame.
Calibration: Calibrate the sensor by running the car on a smooth surface and adjusting the sensor’s sensitivity if possible.
Threshold Setting: Experiment with different threshold values on the sensor to fine-tune the pothole detection.