I have a “safety edge” which is used as a bumper on a robotic rover. If the robot runs into something the edge compresses and I need my arduino to cut the motor.
The safety edge is basically a 8k ohm resistor in normal position and then goes to 0 ohm when compressed. The specs on it say 24v and 10mA which I believe are the maximum values.
I believe this is super easy but I’m always confused about pulling pins low and not sure if this should be driven directly from the pin etc.
I can figure out the software interrupt side of this but what’s the right hardware circuit for this?
The standard way to read a resistance is to place it in a resistive divider circuit, and connect the divider voltage output to an ADC input. The divider can be powered by Vcc on your processor board. If you want to make it a digital input, you have to calculate divider values that place the output voltage in the correct input ranges for a HIGH and a LOW indication. Those are detailed in the processor data sheet. A good ball park guess at the companion resistor would be 1.0k.
Try with the following resistor divider circuit (Fig-1); where, R2 is the sensor that will be compressed.
Figure-1:
When R2 is not compressed, the voltage at A0-pin is : 0.5V.
When R2 is compressed, the voltage at A0-pin is: 2.5V
You can execute the following codes:
void setup()
{
Serial.begin(9600);//// put your setup code here, to run once:
unsigned int y = analogRead(A0);
//Serial.println((5/1023.0)*y);
if((5/1023.0)*y > 2.0)
{
Serial.println("Sensor is pressed.");
//--take actions as needed
}
else
{
Serial.println("Sensor is not pressed.");
//--take actions as needed
}
}
void loop() {}
You should not use an interrupt for this. If you are convinced that you need to use an interrupt, you have some serious problems with your code, for example using "blocking" style code which relies on delay() for timing. You need to evolve beyond that style of coding if you want to make a robot.
It is good idea to use interrupt for this. It is a safety switch. If the program get stuck in an endless loop there is still chance interrupts are enabled and will save the day.
If more safety is needed disconnecting the motors by a HW may be considered.
Arguably safety cutoffs should be hardware only, directly wired, and in such a way that a connection failing triggers the cutoff. Then you only have to prove the hardware is correct, not both the hardware and the software....
Yep it’s exactly like that. It’s super simple…it’s 2 wires with a 8k ohm resistor between it. If it gets pressed the 2 wires get connected directly so the resistance goes to zero.
And just what will this interrupt do if the code has crashed?
An "endless loop" would means the code itself is faulty. A processor crash means it is no longer initialised.
Why would you connect the sensor between two floating points in the circuit. Why not between the input and ground, and why a resistor in series with it?
And why use an analog read (which of course, does prevent the use of interrupts, not that it matters )?
From the datasheet, the 8.2K is shown connected across the switch contacts. If INPUT_PULLUP for A0 is enabled, this will be 30-50K connected to 5V, so using 50K/8.2K there'll be an analog reading of about 144 when the switch is open, 0 when closed. Therefore, just detect if A0 < 50 for a closed switch (else open).