Analog Input ADC vs Oscilloscpe: Discrepancies

Moin,

I am looking for some guidance on the data returned from analogRead().

Photos: https://github.com/Potatoconomy/Relay-Photos/tree/master

Briefly, my goal is to test circuit switching of a relay (G6A) that is controlled by the Arduino Uno. To do this, I breadboarded together a voltage divider that returns a different voltage into A5 depending on which circuit in the relay is connected. I read these voltage changes in the form of a binary message I send through that controls the switch state of the relay.

see link for photo

I also measured this output with an oscilloscope. A relay has a small period of time where the lever needs to switch, creating a momentary open circuit in the board. Here, all 5V should be going through A5 and the scope probe to the GND (So Serial data will be 1023). According to the datasheet, this time period is maximum of 5ms. With the scope, I measured 2.5ms.

see link for photo

However, recording with the Arduino, I need to have at least delay(7) following turning the Relay circuit on in order to not measure values of 1023.

see link for photo

The above photo can be compared to the delay(5) shown below, where the open circuit points are visible.

see link for photo

Any ideas what is going on?

PS: Sorry for not having embedded photos. I had it really prettily formatted, but Arduino Forums does not allow new users to embed photos. Hence the Github Repo for a better format.

https://github.com/Potatoconomy/Relay-Photos/tree/master

//======

const int COIL_PIN = 2;
const int TOP_DATA_PIN = A5;
const int MESSAGE_SIZE = 10;
const boolean MESSAGE[10] = {1, 1, 1, 1, 0, 0, 1, 0, 0, 0};

//=======

void setup() {
  Serial.begin(9600);
  pinMode(COIL_PIN, OUTPUT);
}

//=======

void loop() {
  analogReference(DEFAULT); // 5.0V - 4.9mV resolution
  
  int i;
  for (i = 0; i < MESSAGE_SIZE; i++) {
    write_bit(COIL_PIN, MESSAGE[i]);
  }
}

//=======

void write_bit(int pin_number, boolean current_bit) {
  // Writes HIGH or LOW to corresponding pin
  digitalWrite(pin_number, current_bit);
  delay(7); // Relay requires 5ms switching time, actually only needs 2.5ms according to scope
  get_analog(TOP_DATA_PIN, 10);
}

//=======

void get_analog(int pin_number, int n_print) {
  // Reads analog data from selected pin and records it.
  int i;
  for (i = 0; i < n_print; i++) {
    Serial.println(analogRead(pin_number));
  }
}

//======

post a schema of your circuit and post the pictures here

The contact bounces for some time.

Yea I've tried to do this many times, but I'm always met with the response, 'Sorry, but new users cannot embed photos'. Therefore I was forced to make a Github repo that is nicely formatted with the necessary photos in the README.md. If you know how I can bypass this restriction, I'd be happy to implement it :slight_smile:

https://github.com/Potatoconomy/Relay-Photos

Which is why I used an oscilloscope to measure this more precisely. The switch time is <5ms, actually around 2.5ms on the oscope and then the contact is secured.

Are you measuring the same things?

With the scope, are you measuring time from when the trace on A5 starts going up, or from when the pulse on pin 2 begins to rise?

Pin 2 controls the Relay switching coil. The scope probe is hooked on the 2k Resistor, as is the A5 wire.

I understand.

With the Arduino code, you are measuring time from the digitalWrite on the relay pin to the time it takes to read and serialwrite the observations. With the oscilloscope you don't seem to have the relay input start as reference, so you seem to be measuring different things.

Ah. Excellent observation! I will put a 2nd scope probe on the relay coil to get the trigger down. I'll update the thread after this is done.

In you trace
image
is the relay coil going from energized to unenergized where my arrow points?

If yes then its because the magnetic circuit of the relay coil needs to get rid of the magnetic energy that it was holding when the coil was on.
You need a diode across the relay coil.

Oscope_Analysis_zoomed
Arduino no delay analog reads red is coil

You were right, I believe. There is a significant time delay from when the Coil voltage reaches 5V and to the point when the lever in the relay switches to open circuit mode. Now remain just two questions I can think of.

  1. What physics causes this delay in the lever moving?

  2. The oscilloscope shows a bit of negative voltage in the turning off of the coil (back current from stored coil energy), is this bad for the arduino pins?

Ad 1. Coil has inductance and so current lags voltage. Strength is proportional to current, it takes some time for enough current=strength to build up in the coil to grab the lever.

Ad 2. It is bad if you don't have a freewheel diode across the relay. You should have one and some resistor between the relay and Arduino pin. 1k should be a reasonable value.

Both magnetic delays (flux has to decay), and mechanical inertia. You can affect the first by choosing a fast snubbing circuit using a zener (rather than just a simple free-wheel diode), but the second is unavoidable.

You should always have some snubber on a coil, otherwise voltage spikes in the 100's or 1000's of volts can be generated - bad news for your circuit.

Thanks. Building the circuit in this manner appears to resolve the back emf issue. What I found a little strange was that both diodes (2x IN4001) were necessary to stop it. I would have expected that the first diode in series would have been enough.

I wonder what exactly you want to measure.

Back-emf voltage of the relay coil can easily reach reverse breakdown voltage of a small diode.
Therefore a series diode is not recommended.

Remember that the connection wires to the relay also have inductance.
Where you have the diode and where you measure also matters.
A 1N4148 could be slightly faster than a 1N4001, and a 1N4007 could be slightly slower.
Leo..

Even the most powerful diode is useless when in series with the inductor. It let the voltage transient to pass, providing no protection at all.

You don't, and can't.
Just give it a safe path to flow, through the diode across the coil.
Leo..