Hi, could someone please help me understand what's wrong with my project? (Sorry if I put the topic in wrong category, not sure which would be the right one, so I picked general).
So I have Arduino Mega board, powered with an external power source (step-down converter from 12 volt to 5 volt) via VIN pin. I looked at the doc and it seems like the right way to do it. Power source outputs about 5.06 V, and the input pin is connected to 12 volt with voltage divider, R1=47kOhm, R2 = 20kOhm.
I am trying to get a 12V voltage reading from pin A3.
I have a loop which reads the values from analogRead and stores them in array, and then I take an average:
std::vector<int> data_buf;
data_buf.resize(size);
for (int i = 0; i < size; ++i)
{
data_buf[i] = analogRead(A3);
}
avg = 0;
for (int i = 0; i < size; ++i)
{
avg += data_buf[i];
}
avg /= size;
The problem:
At this stage, I have wrong avg value, but ONLY if I disconnect usb cable from computer and leave it to run on 12v->5v power source. If I connect usb cable back, the readings go back to normal.
I checked voltages with a multimeter. Voltage across VIN and GND is 5.06V in both cases (with and without usb cable connected), and voltage across input pin A3 and GND is 3.55V - 3.56V in both cases as well.
However, I get avg=732 with usb cable connected (with seems correct - 732 * 5 / 1024 = 3.57), and without usb cable, I get avg=868, which is way off (868 * 5 / 1024 = 4.24).
I also tried two different arduino boards and got exact same results.
There's also TFT screen with RA8875 controller connected to arduino. It is being powered by the same step-down converted as arduino itself.
Your word description is imprecise, a schematic would be much better; if I read your text correctly, you're feeding 5V into Vin, which is way too low for Vin; when you connect USB, the power comes from USB, so the board works correctly.
Feed Vin with at least 7V.
The OP has already been told, feed at least 7V to Vin. If they do that, no problem. The issue becomes powering significant auxiliary circuits, as we know the 5V regulator onboard is limited.
To do that, the OP should use his 5V source to power the auxiliaries, connecting it's negative to the negative of the Arduino for reference.
FWIW, I use 5V from a supply to feed the Nano 5V in/out, never a problem, but I do realize that a) the Mega has a different power circuit, and b) the Mega recommendations from Arduino really should be followed.
Leave existing 5v power supply to power everything else besides Arduino (tft display and RA8875 controller)
Add another power supply specifically for Arduino, which outputs correct voltage to power Arduino's VIN (7-12 volts according to doc, can't believe I missed that, lol)
Did I understand everything correctly?
Also, a question: does it matter exactly which voltage in specified 7-12 volts range is supplied to Arduino? Or I should be good as long as it's inside that range?
Yes, but perhaps no. Consider, the regulation circuit converts 7v to 5v, or 12v to 5v. If it's a linear regulator(I believe it is), the extra voltage is simply wasted power, at whatever current your 5V circuit is drawing. Wasted power is heat; at 12 V your regulator runs hotter than it would at 7V. Does it matter? If you enclose the circuit in a box, the whole thing will run much hotter due to that power dissipation. If it's in free air, then it's just the regulator that works harder.
Now, if you want to power anything from the 5V pin of the Mega, you need to consider how much extra current that external circuit is drawing, and how much extra heat will be dissipated by the regulator as a result. Since these calculations are beyond many newbies, this is why we generally recommend the Arduino NOT be used to power anything but the simplest of circuits, such as a few buttons and LEDs.
Closer to 7V will release less heat in the regulator...
This will allow more stuff (like leds, small screens, not motors or relays) driven directly from the arduino...
Thanks, I'll keep that in mind. I am not going to power anything extra from the Arduino itself, since TFT display requires it's own power supply anyway, so I'll just use that if needed.