Sanity check -- Is common ground needed here?

Hello all.

I am designing (or was, now I’m mostly pulling my hair out) a circuit composed of a HUZZAH ESP32 MCU which controls two stepper motors and 3 IR sensors. I posted initially in the project guidance forums some time ago and was given some good advice. Now I am trying to deal with some issues that have come up in the process.

The project requires that the power source and MCU be self contained, and controllable by WiFi. The ESP32 was an obvious choice… but I am not so sure anymore. The problem stems from the fact that ESP32 operates on 3.3v, and as such that is all it can offer my system. I am driving the stepper motors using 9v batteries and Pololu’s MP6500 driver anyway, so I figured I can use a voltage regulator to shift that down to the 5v required for most sensors.

This was the diagram I came up with:


The output has been extremely noisy, and in my investigations into how it could be reduced all kinds of strange behavior started to crop up.

Using this code:

#define test A3

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
delay(1);
// put your main code here, to run repeatedly:
float volt = analogRead(test);
volt = volt*3.3/4096;
Serial.println(volt);
}

I looked at the serial plot and saw massive spikes of up to an entire volt happening every few readings:

I tested the voltage coming into the regulator: ~8v as expected
And the voltage coming out: ~5v as expected

However, not only is the behavior wildly spiky… it is extremely inconsistent. I see 3 kinds of behavior from the circuit, without changing anything. Very spiky data that is useless, a gradual increase in the voltage over time until it is unloaded and set back to zero (only to ramp up again over time), and, for some reason, I occasionally get very clean data. It sometimes feels as though touching something makes the system behave completely differently.

If I shorted the capacitor for a moment, odds were good that the data would suddenly become clean. I discovered this when I bumped my finger across its legs and glanced at the serial plot. However, it would also frequently spark, and this just felt like bad practice… so I stopped doing it.

My question for this forum lies in the fact that the IR sensors are passing a signal to the ESP32. I read the stickied thread about common ground, and now realize that this may apply to my situation. However my confidence in what is real has been destroyed today so I figured I would just ask.

To make matters worse, the robot seemed to be functioning with a very simple script two days ago: Hmm - YouTube

But now the data is unusable a good portion of the time.

I would appreciate any insight anyone has.

Data sheets:
IR Sensor: sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a51sk_e.pdf
ESP32: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-huzzah32-esp32-feather.pdf?timestamp=1605590713
Regulator: Pololu 5V, 2.5A Step-Down Voltage Regulator D24V25F5

GND should always be common as it it the base from which all voltage is measured.

Sometimes there are various schemes to reduce noise by attaching grounds in different configurations, but as a start you should connect all the GND together.

Thanks for the clear and detailed post with schematic & data sheets.
Yes you absolutely need a common ground. But ..
the motors are taking AMPS so the way your grounds run will be important.
You have seperate 9V, 5V and 3.3V circuits
The grounds for those circuits should be connected at one single point (star point), ideally the battery negatives and the 0V at the regulator.

A photo or 3 of what you have built would help us to see if there are any obvious changes to the wiring that would reduce the noise.

Which 9V batteries are you using - they aren't normally upto to powering any motor, but I see
you have several in parallel and rated at 1.8Ah, which seems unusual.

I would run a GND wire from the 9V GND at the voltage reg to the feather's GND and put 0.1uF ceramic caps on both sides of the reg.

fawltyplay:
now I'm mostly pulling my hair out

It's beneficial to switch frame of mind. Instead, enjoy having something challenging to sort out. We usually get there in the end. When you really get stuck, then some assistance like on here is going to help for sure.

I looked at the serial plot and saw massive spikes of up to an entire volt happening every few readings:

I looked at the serial plot and saw a relatively smooth and stable signal. Peak-peak change is 2/1023 = 0.2% ... Not bad!
Edit: Whoops - maybe I should've looked at your code first!

GP2Y0AS1SKOF Distance Measuring Sensor Unit

OK, I wouldn't expect an IR-LED reflective distance sensing device to give a stable signal. Too many variables exist. I mean, this isn't a high end laser measurement device. I suspect you'll need to use signal conditioning in your code.

Wow, a ton of replies. Thank you all, I'll try to address everything that's been brought up.

marco_c:
GND should always be common as it it the base from which all voltage is measured.

Sometimes there are various schemes to reduce noise by attaching grounds in different configurations, but as a start you should connect all the GND together.

johnerrington:
Thanks for the clear and detailed post with schematic & data sheets.
Yes you absolutely need a common ground. But ..
the motors are taking AMPS so the way your grounds run will be important.
You have seperate 9V, 5V and 3.3V circuits
The grounds for those circuits should be connected at one single point (star point), ideally the battery negatives and the 0V at the regulator.

JCA34F:
I would run a GND wire from the 9V GND at the voltage reg to the feather's GND and put 0.1uF ceramic caps on both sides of the reg.

This makes sense... is the procedure really to simply run a GND wire connecting all the GNDs in my system?

The wiring diagram for the MP6500 and other stepper motor drivers seems to indicate this is unnecessary, though I understand that working for other circuitry. They also handle all of this within the board however, and I assume the designers get around the common GND problem using clever circuitry (switches?). That being the case, would connecting those GND cause problems?

MarkT:
Which 9V batteries are you using - they aren't normally upto to powering any motor, but I see
you have several in parallel and rated at 1.8Ah, which seems unusual.

I am using some rechargeable 9v batteries... They seemed to be the most stable option to use with a low dropout voltage regulator while also slotting in nicely to the voltage requirements of the MP6500. They are only 600mAh each, but I've managed to fit four of them onto the limited footprint of the robot, giving me 2400mAh of total current (roughly, I think). That label on the drawing is slightly outdated...

Side note, they seem to drive the motors fairly well. The motion isn't exactly super smooth, but I also haven't tried to make it be yet.

Southpark:
It's beneficial to switch frame of mind. Instead, enjoy having something challenging to sort out. We usually get there in the end. When you really get stuck, then some assistance like on here is going to help for sure.

I started out this way, but now my source of stress lies in the fact that I have a deadline to meet, and I have not even begun to work on the more complicated part of programming the robot itself as I feel stuck and out of my depth on this portion. A night's sleep has done me some good but it's hard not to worry at this point.

I will post some images of the robot itself when I get home, but I can tell you that the wiring is a mess. The cabling from the stepper motors is bundled near the sensors and their wires, up to the drivers which are loose in the air, whose wires from the ESP32 run adjacent to the sensor wires... it's a bit gross looking, even for me.

dlloyd:
I looked at the serial plot and saw a relatively smooth and stable signal. Peak-peak change is 2/1023 = 0.2% … Not bad!
Edit: Whoops - maybe I should’ve looked at your code first!

GP2Y0AS1SKOF Distance Measuring Sensor Unit

OK, I wouldn’t expect an IR-LED reflective distance sensing device to give a stable signal. Too many variables exist. I mean, this isn’t a high end laser measurement device. I suspect you’ll need to use signal conditioning in your code.

Yeah. Unfortunately the IR sensor is one of the few modules that can give me any reading at all at the distances I need.

A minimum range of around 2cm is a pretty hard requirement, and a high update rate is good. The HC-SR04 would do, but it’s so large that I don’t know how I would fit 3 of them in a comfortable way.

I was anticipating noisy data that could be reduced via the use of a large capacitor, and sample averaging. And when the data does come out clean, it looks quite nice. A very usable mostly smooth line with some variation. Only I can’t get it to be consistent.

We don't know your setup for testing, but some of the variables would be reflections, glossy surfaces, changes in ambient light, vibration or other movement, or electrical effects of from other devices and components.

I would suggest testing a single sensor alone against a flat opaque surface (i.e white paper) in a darkened room. Try at different distances to see what the signal level and stability is. I suspect it would be much improved.

Its possible the stepper motor or other components are interfering. You may need a more rugged battery power source or perhaps separate batteries for the motors and electronics. Note that a lot can be done in code to smooth things out if needed.

EDIT:

Do you know what occurred within the circled area?
If its just power-up, not a problem.
If its turning on or off of the stepper motor, then the 1V or so level shift in the signal is an issue.

dlloyd:
We don't know your setup for testing, but some of the variables would be reflections, glossy surfaces, changes in ambient light, vibration or other movement, or electrical effects of from other devices and components.

I would suggest testing a single sensor alone against a flat opaque surface (i.e white paper) in a darkened room. Try at different distances to see what the signal level and stability is. I suspect it would be much improved.

Its possible the stepper motor or other components are interfering. You may need a more rugged battery power source or perhaps separate batteries for the motors and electronics. Note that a lot can be done in code to smooth things out if needed.

Thanks for continuing to respond to the thread.

I tried reducing the environmental noise before going to bed, and the results weren't visibly different. I also tried running the sensors without triggering the motors to take steps. The signal was similarly unstable, mostly displaying that same "buildup" of the voltage reading over a period of time before it falls off a cliff back to zero and repeating. Resetting the board would sometimes cause it to run "properly." I also tried running the sensors off a single 9v battery (a circuit completely separated from the motors) and I got even worse results. Is there a reason that should be the case? In every test I made sure to check the voltage throughout the circuit and everything seemed to be more or less nominal with little to no fluctuations.

I just now tried with an Arduino Uno using its 5V rail (and thus a common ground) and the signal looks beautiful:



And this is with only a large (2200uF) capacitor! I used the same code as above but modified the conversion line to represent the 0-1023 resolution of the Arduino Uno analog input ports.

Now trying with the ESP32 powered by a USB-C cable and the sensor driven by the 9v battery regulated to 5v (no common ground yet):


And finally, with the common GND (green wire):

And now I feel much better. If this was too many images to a post let me know, I just figured I would provide them in case someone else stumbles upon this thread in the future. Or I forget what I've learned.

I'll move this into the full setup and check out the signal quality with the motors stepping shortly.

If this all works out, I'm still very curious as to WHY the behavior I was getting would occur. What is it about the lack of a common ground that causes voltage buildup over time?

dlloyd:
We don't know your setup for testing, but some of the variables would be reflections, glossy surfaces, changes in ambient light, vibration or other movement, or electrical effects of from other devices and components.

I would suggest testing a single sensor alone against a flat opaque surface (i.e white paper) in a darkened room. Try at different distances to see what the signal level and stability is. I suspect it would be much improved.

Its possible the stepper motor or other components are interfering. You may need a more rugged battery power source or perhaps separate batteries for the motors and electronics. Note that a lot can be done in code to smooth things out if needed.

EDIT:

Do you know what occurred within the circled area?
If its just power-up, not a problem.
If its turning on or off of the stepper motor, then the 1V or so level shift in the signal is an issue.

Just saw your edit—I do believe that was power up, though I did not take the time to label my graph screenshots as I should have so I can't be sure anymore.

What is it about the lack of a common ground that causes voltage buildup over time?

No common reference point ... the voltage level GND1 can float away from GND2.
Same as trying to measure a high impedance (high-z) signal ... lots of noise, floating values, meaningless readings.

Update, this seems to have helped a lot. Without the motors running:


Very clean signal. Running the motors using the following code:

//LEFT STEPPER
#define STEP_PIN2 33
#define DIR_PIN2 15

//RIGHT STEPPER
#define STEP_PIN 32
#define DIR_PIN 14

#define SenL A2
#define SenC A3
#define SenR A4
 
bool dirHigh;
 
void setup()
{
  dirHigh = true;
  digitalWrite(DIR_PIN, LOW); //HIGH: RIGHT WHEEL FWD
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIR_PIN2, HIGH); //LOW: LEFT WHEEL FWD
  digitalWrite(STEP_PIN2, LOW);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN2, OUTPUT);
  pinMode(STEP_PIN2, OUTPUT);
  Serial.begin(115200);
  
}
 
void loop()
{
    float volts1 = analogRead(SenC);
    //float volts11 = analogRead(SenL);
    //float volts21 = analogRead(SenR);
    delay(1);
    float volts2 = analogRead(SenC);
    //float volts12 = analogRead(SenL);
    //float volts22 = analogRead(SenR);
    delay(1);
    float volts3 = analogRead(SenC);
    //float volts13 = analogRead(SenL);
    //float volts23 = analogRead(SenR);
    delay(1);
    float volts4 = analogRead(SenC);
    //float volts14 = analogRead(SenL);
    //float volts24 = analogRead(SenR);
    delay(1);
    float volts5 = analogRead(SenC);
    //float volts15 = analogRead(SenL);
    //float volts25 = analogRead(SenR);
    delay(1);
    float Voltavg = ((volts1))*3.3/4095;
    Serial.println(Voltavg);
    //delay(100);

    if (Voltavg < 1.2) {
      // FWD
      digitalWrite(DIR_PIN, LOW); //HIGH: RIGHT WHEEL FWD
      digitalWrite(DIR_PIN2, HIGH); //LOW: LEFT WHEEL FWD
      
      digitalWrite(STEP_PIN, HIGH);
      digitalWrite(STEP_PIN2, HIGH);
      delay(2);
      digitalWrite(STEP_PIN, LOW);
      digitalWrite(STEP_PIN2, LOW);
      delay(2);
    }
    if (Voltavg > 1.2) {
      // REVERSE
      digitalWrite(DIR_PIN, HIGH); //HIGH: RIGHT WHEEL FWD
      digitalWrite(DIR_PIN2, LOW); //LOW: LEFT WHEEL FWD
      
      digitalWrite(STEP_PIN, HIGH);
      digitalWrite(STEP_PIN2, HIGH);
      delay(2);
      digitalWrite(STEP_PIN, LOW);
      digitalWrite(STEP_PIN2, LOW);
      delay(2);
    }
}

And I get some very clean output. Thanks guys!

If this all works out, I'm still very curious as to WHY the behavior I was getting would occur. What is it about the lack of a common ground that causes voltage buildup over time?

I'd reverse that question with an analogy:

If you don't moor both boats to the same riverbank, why would you expect them to stay level with each other?

With isolated systems it doesn't take much charge build up to create a big voltage difference (this is
static electricity territory). When the air is dry you might see voltages of thousands of volts build up
due to friction transferring small amounts of charge from one isolated system to another.

With current electricity you have everything in a loop and charge flows round circuits, and voltages are
defined between two points in the same circuit - they are relative.

If you have two circuits connected by a single wire, then current can't flow in that wire (at DC).
The wire itself becomes the reference point between the two circuits. Only if that is a ground
wire do the voltages in the circuits become compatible with your expectations!

MarkT:
compatible with your expectations

Now, that's a phrase worth remembering...

What is it about the lack of a common ground that causes voltage buildup over time?

Another consideration - the influence of the measuring circuit. Here, its the analog input's internal sample/hold capacitance trying to charge (or slowly charging) with a static/stray signal.

If the voltage buildup always happened at a consistent rate:
I guess a mad experiment (if your still curious) would be to slow down the sample rate (put a larger delay between measurements) to see if it slows down the rate of voltage buildup. If it doesn't, then never mind. If it does, then I guess you could blame the measuring circuit for the buildup. :slight_smile: