I'm a new person to the Arduino world. I need help writing code to control a external relay module on a change in an input dc voltage. The normal input is 3 vdc. I need to activate the relay module (break contacts) if the voltage input goes high to 3.1 vdc or low to 2.9 vdc. This is being used as a safety interlock to control an external 120 volt amplifier. I know how to connect the Arduino board to the external relay board and I have the 3 vdc control wire from the amplifier but I don't know how to write the code to protect the amp as stated above.
Any help is greatly appreciated! I apologize for the beginner question.
Jim
In simplest terms you can try something like this:
#define UPPER_LIMIT 3.1
#define LOWER_LIMIT 2.9
//change to suit your circuit as needed
#define NORMAL_RELAY HIGH //normal operation
#define SAFE_RELAY LOW //when amp needs to be "safed"
const byte pinInput = A0;
const byte pinRelay = 2;
int currReading;
float volts;
void setup()
{
pinMode( pinInput, INPUT ); //not strictly needed, provided for clarity
pinMode( pinRelay, OUTPUT );
digitalWrite( pinRelay, NORMAL_RELAY );
}//setup
void loop()
{
currReading = analogRead( pinInput );
volts = (float)currReading * 5.0 / 1024.0;
if( volts >= UPPER_LIMIT || volts <= LOWER_LIMIT )
digitalWrite( pinRelay, SAFE_RELAY );
else
digitalWrite( pinRelay, NORMAL_RELAY );
}//loop
You can add some more functionality (and complexity) if you like; for example, if the control voltage goes high or low, the relay is safed and it stays that way until there's a user "reset" input. Perhaps you add a time element; if the relay is safed a minimum amount of time has to pass before it is re-enabled. Maybe you add some hysteresis to the system so the SAFE and NORMAL points are not one and the same.