Whe does my project work when plugged into USB but not on battery!

Hi all,
I have a problem that’s been driving me nuts for weeks. I can’t figure it out.
I am building an R/C transmitter for model planes. I’m using a Nano as the encoder board, doing analog reads of the stick inputs and converting to a PPM stream to be fed to an RF module to transmit to the receiver (It’s an FrSky hack module for those interested.)
My problem is this. I’ve been using Xoscope to look at the PPM output signal from the Nano. I can display it when the Nano is plugged into the computer by USB cable (and is therefore powered by the USB), or I can unplug the USB and power the Nano from a battery, and still display the signal on Xoscope. I am using my sound card to communicate to Xoscope, and I have found that, when using the battery, I have to connect one side of the input (microphone) to pin10 of the Nano, and the other side to Nano ground. Not entirely sure that’s kosher but it seems to work. I don’t need the ground connection when the USB is plugged in, presumably because there is a ground connection via the USB cable.
The problem is this. When the Nano is plugged into the PC via the USB cable, it works perfectly. In the screen shot it can be seen that all the pulses (eight of them) are present and correct. However, when I unplug from the USB and connect the battery it’s a different story. Now the first three channels (the only ones with analog reads in my test program) produce pulses that are way too wide, about 5,000us. The other five, which are hard coded to 1500us (it’s 3000 in the code as the size must be doubled for the PPM generator timer) are spot on.
If I hard code the values to 3000 for the first three channels as well, they are also correct. It’s only a problem when I read an analog input and map it to my output values.
I ran another test as well to write the values from the first three channels to EEPROM, conditional on a switch setting. So when I plugged it into the battery I had the switch on and the values were written to EEPROM. I then unplugged it, turned the switch off, then plugged in the USB and read the values from EEPROM. The values are correct. This was the only way I could think of (given that serial monitor was not available without the USB plugged in) to see what values were being generated by the analogRead and map functions while it was running on battery.

So, why does it work when connected to the USB but not the battery? Do I have to add some resistors or capacitors or something? Do I need to feed 5v to aref? Does the board behave differently when powered from a battery instead of the USB? Mine certainly seems to…

It also doesn’t work when I hook up the PPM output to my RF module and switch on my receiver. The servos move slightly, but don’t respond to stick input. I thought it might be some weirdness with Xoscope, but the fact that the servos don’t respond to stick input would seem to indicate a problem elsewhere.

I’ve attached a copy of my testing program which does basically nothing except read the first three adcs (aileron, elevator and rudder on my transmitter) and feed them to the PPM generator.

I’ve also attached a couple of screenshots of the Xoscope output. Battery.png shows what it looks like when running on the battery. USB.png is with the USB plugged in (and what it should look like).
The others are a couple of shots of my transmitter, for no particulare reason. The battery pack is an eight cell NiMh pack (Sanyo Eneloops, excellent quality and show 10v on my multimeter), the FrSky hack module you can just see stuck to the right hand side witih double sided tape. The gaggle of wires on the circuit board connect all the adcs to the pots in the box. The yellow wire is on pin 10 and is the PPM output. The pcb switches will one day be reversing switches to reverse the channel direction. The round black thing on the left is a momentary button, that can be held down on power up to do various rare functions, like stick calibration or whatever else I think of.

All advice gratefully received,
Ian

PPMtesting.ino (2.54 KB)

What type of battery are you using ?

this could be a grounding issue or a power issue (a voltage drop due to the protection diode that impacts the analogRef for example or underpower later on some of your connected components - or just not enough amps/watts ...)

Wow you guys a quick! As I mentioned in the post I'm using an eight cell NiMh pack, nominally 9.6v but reads 10v on my multimeter. It's currently fully charged (pardon the pun). ;-)

On a Nano there is a difference for 5V when your board is powered by USB versus Vin

There is a shottky protection diode (in series) with the power coming from USB which means Vcc will be at 4,4 or 4.5V and when you power through Vin (between 8 and 12V) then the regulator will give you really 5V

So when you do a AnalogRead on a Nano powered by USB with the standard reference, then your measure is wrong as the top voltage won't be a pure 5V, it will be impacted by the voltage drop in the diode, which is temperature dependant so hard to compensate. Measuring when powered on battery is actually more precise.

Note also that you would be better off reading twice each analogRead() and discarding the first one to ensure the circuit has time to stabilize

But when you are connected to USB, you are likely grounded to a real earth whereas on battery it's all relative. How are things connected / grounded ?

How do you power the Servos?

Hi J-M-L,

Thanks for the detailed reply.

Last things first, the board is not powering any servos. I do use the same batteries to power my receivers though, and a 5 cell pack (nominally 6v) has no problem powering four digital servos working hard. They are 2000Ma/Hr batteries so in theory they can deliver 2 amps. In reality they could probably deliver more than that for short periods, but then of course there's the risk of a severe voltage drop which may cause the receiver to go into failsafe. The FrSky RF module is powered directly from the battery, not via the Nano.

The program I uploaded is just for testing, so only does a single analog read. My real program does multiple analog reads but sill exhibits the same behaviour. I tried averaging multiple reads but found there were still huge spikes. I'm currently using a library called RunningMedian that seems to work quite well and is easy to use. It introduces a slight lag but the output is much smoother.

When running on the battery the pots have one side connected to the Nano's ground pin and the other side of the pot is connected to the Nano's 5v pin as per every example I've ever seen. At least, that's what I thought! Now I've double checked, and the pots are connected to the battery positive, not 5V! Maybe that's the problem! Out with the soldering iron, I'll post the results soon.

;-) Ian

Worth exploring indeed ! :)

Well J-M-L my problems have gone away. Wiring up my board CORRECTLY has solved the problem.

Thanks for your help, it at least got me to recheck everything and find the error.

:-)

Great - sometimes it’s the obvious we don’t see :)