Measuring analogue voltage/bandgap/differential

I'm building a pulsed electrical discharge machine.
I trying to find a way in code of comparing an analogue input to see if it falls within a +/- positive voltage range.

For example if i read 3v in on the input i want to see if it falls within the range of 2.5v and 3.3v, if it does then do nothing or if below the lower threshold it would provide a result which drove a motor in one direction and if above drove a motor in the opposite direction.
in effect a window comparator.

My intuition, knowledge, skills and google are all letting me down.

your help would be much appreciated, a code snippet would lead to an explosion of joy.

a code snippet would lead to an explosion of joy.

Start with

int aValue = analogRead(A0);

I hope that the explosion did not cause too much damage

Cheers bob :slight_smile:

Seems to simple, is there a better way

i thought about both conditions ORed together then realised I want two outcomes.

int sensorValue = 0;
unsigned int lowLimit = 307;    // Analogue lower threshold = 307 30% 1.5 V if Vsupply = 5 V
unsigned int highLimit = 717;   // Analogue lower threshold = 717 70% 3.5 V if Vsupply = 5 V
void setup() {

void loop() 
  sensorValue = analogRead(A0);
      if (sensorValue > highLimit)
      else if (sensorValue < lowLimit)

nicked idea from plcLib

As you need two outcomes it seems sensible to keep the code simple and readable

Thanks again Bob.

I'm now going to look at implementing PID on the outcomes, but may end up ditching the idea as its possibly going to be resource hungry and just use the two error signal directly.


What is the frequency of the analog input signal ?

Sorry ard_newbie i have not been back to the post for a while.

The analogue signal from the voltage arc is DC
the driver pulse is a variable mark/space square wave, the generated arc voltage during the on time is what is being measured.

this question relates to the band gap which i want arduino to handle instead of external electronics.
the limits dictate weather the motor should drive the electrode into the work to maintain an arc or retract the electrode if it shorts out.