Serial port "freezing" for voltage measurement after starting the motor with a button push

Hello there. I've got the following problem when trying to measure the voltage from 9V Crona after running a motor. The point is that I have a mini breadboard with a button and contacts that connect that motor, 9V Crona and voltage sensor. First, when I upload the program I can see the current voltage on the screen (about 7-8 volts), but when I push the button and start the motor, voltage decreases to 2-3 volts and then the serial port outputs that smaller voltage value and then just "freezes" not showing anything until I upload the program again.

Here is the program code which I use:

float R1, R2;
int a = 0;

void setup() {  

    pinMode(A0, INPUT);
    
    R1 = 30000;
    R2 = 7500;

    Serial.begin(9600);

}

void loop(){
 
    int a = analogRead(A0);
    float b = a * 5 / 1024.0;
    float c = b * (R1 + R2) / R2;

    Serial.print("Voltage: ");
    Serial.println(c);
    //Serial.println(c * 100 / 9);
    delay(1000);

}

What have I been doing wrong? What are any possible causes of that problem? Please, let me know if you need any further details on this matter.

Here are some screenshots that may help:


d9c1a6b1dab7c8be7547c4f494e790d31a7eedc4_2_690x216

"Battery"

Your display is showing a "load" of the motor pulling the true voltage down... to a point your Arduino reboots. Use a dedicated battery pack of 5 or 6 x AA (5 x 1.5 = 7.5vdc, 6 x 1.3 = 8 vdc) for the motors.

1 Like

Post a diagram showing your connections, especially how you are powering the Arduino and the motor. You can draw by hand and post a picture

1 Like

Use a proper power supply, a 9V block battery isn't suitable to run motors. Also it seems your battery is just about exhausted: you start with a voltage of <8 V.
Your motor is drawing more power than the battery can deliver even if fully charged.
If you must use batteries, use 4xAA alkaline or 2x LiPo. Otherwise, use a wall power supply (power adapter - phone charger, or from some old device, or whatever - as long as it produces DC of suitable voltage it's fine).

1 Like

That is what I would expect to happen, you do not have enough power to operate it. If you have connected the motor directly to the Arduino you might be lucky and haven't destroyed it. Here are some simple rules:
Gil's Crispy Critter Rules, they apply to processor hardware:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Rule #3 Don't connecting or disconnecting wires with power on.
Rule #4 Do not apply power to any pin unless you know what you are doing.
LaryD's Corollarys
Coro #1 when first starting out, add a 220R resistor in series with both Input and Output pins.
Coro #2 buy a DMM (Digital Multi-meter) to measure voltages, currents and resistance.
Violating these rules tends to make crispy critters out of Arduinos.
Hint: It is best to keep the wires under 25cm/10" for good performance.

mancera1979, thank you for your reply. Here is a TinkerCad simulation of what I am trying to do. There when pushing a button, the voltage decreases, but in reality it decreases too, though after that serial monitor stops working. Please, let me know if you need more info on this topic.

Unfortunately I cannot log into Tinkercad.

I may suggest you to do the following, to ascertain if it is the serial port that is freezing, or the Arduino is resetting:

Use the builtin LED to inform you if the program is OK.

For example, you can make it blink five times per second for a couple of seconds in setup () so you if/when the Arduino restarted and then a short blink in your loop() once per second so you know the program is running.

In setup() you have to use delay(), as it is the only way to do it.

In loop() avoid using delay(). If you don’t know how to do this check the Blink Without Delay in the IDE Examples section.

mancera1979, here is a rough sketch of what I have been trying to do.

In general, my idea is to know how much energy is there for my robot until it needs some new power supply. I just want to display it on the screen afterwards. How actually may I do that? I know I can measure voltage only on the separate points of the curcuit, but how can I decide on what the whole system needs in the terms of energy? Is that a very difficult problem?

Please, try this link: Circuit design Motor_Push_Button_Plus_Voltage_Measurement | Tinkercad

Thank you, I will try this option as well and let you know.

Thank you very much. I will try this option too.

And sorry for the late replies, guys. Still waiting on your advice about my general question mentioned in the post #10.

It's very simple, actually. Build the robot, then measure its power consumption.
Power consumption = voltage (at battery/power supply terminals) * current supplied by battery/power supply.

Search for "arduino data logging" and you will find SD Cards in use. Log the power level and when you recover the SD card, you should see the last power measurement.

Dronebotworkshop shows how to use the SD card...

Look under the heading " Servo Motor Recorder & Playback" to see datalogging used as a record/playback.

I used a battery back with 6 x AA, 1.5V each, but it didn't help. When I run the program, serial monitor shows ~ 9V (full charge, I've just unpacked it), but when I start the motor according to the sketch in the post #9, serial monitor shows a little bit lower voltage, and then stops outputting anything. Before I push the button, TX LED is blinking on the board each second (I use delay 1000ms), but right after I start the motor the LED switches off. Any other ideas on what actually may be wrong? I just can't measure the current energy state of my robot when the motor is runnung and thus I won't be able to measure the energy state of the whole system.

Your power connections are not correct. Use one power for the motors, use another power for the Arduino.

This looks like you have the voltage sensor in series... Are you certain this is how to use the voltage sensor? Voltage is usually measured in parallel and current is measured in series.

[edit]

I found a voltage sensor... I think it is similar to your sensor... I see it is connected in series.


From this site (making a voltage monitor)

xfpd, and where should I add the motor connections in the mentioned above scheme? By the way, there is no any problem in measuring it without the motor. The issue is when I run the motor. And it looks like I should catch that condition somewhere in the code, but don't know how to do that.