Reading a negative voltage with ADC

Steve2381

Senior Member
Hi all

Probably too late in the evening to even think about this... but hey

I have an input that varies from -12.4v to zero (its the voltage from a commercial RGB led controller). The leds have a common 12v supply (well... 12.4v).
Going to stick the input through a in4001, so that will drop a little of voltage off (0.8v?)

How do I read this with an ADC input which is only 0-5v range? I believe I can do it through a resistor network, but whether that is the best way.... I don't know.
I think I take the negative input through a resistor to the Picaxe and then take one resistor to 5v and one to 0v from the same Picaxe pin? What these resistance would be... I have no idea.

Probably a better way would be to use an op-amp (not that I know how to do that either!)

My simplest solution so far?.... Led on the negative supply opposite a light dependent resistor in a sealed tube. Works remarkably well - just a bit laggy

Any advice greatly received.
 
Last edited:

AllyCat

Senior Member
Hi,

If you know the PICaxe supply voltage (which can be measured using CALIBADC if not) then you only need two resistors, say 33k from the negative voltage to an ADC pin and 10k from the ADC pin to the PICaxe supply rail.

Use READADC{10} as usual, but then subtract the variable from 255 {or 1023} to give the value across the 10k resistor. Calculate the voltage across the full divider chain as usual, but then subtract the supply voltage before calling it the (negative) supply voltage (i.e. by putting a "-" in front).

Cheers, Alan.
 

grim_reaper

Senior Member
My simplest solution so far?.... Led on the negative supply opposite a light dependent resistor in a sealed tube. Works remarkably well - just a bit laggy
While an excellent learning tool and invaluable in your education, this method is a bit old hat!!
If you want to get a faster response, replace the LDR with a photodiode or other device with a faster response time.
What's you've made there is an opto-coupler - have a read about them, they can be quite useful for a lot of situations such as this.
(I'm not suggesting you use an opto-coupler - follow AllyCats advice!!)
 

Steve2381

Senior Member
Thanks guys. Yes... I know the LDR was 'old school', but it was my quick, safe, bits-to-hand fix last night!
Googled how to do this for a while then basically got lost in a world of voltage dividing confusion
 

BESQUEUT

Senior Member
If you know the PICaxe supply voltage (which can be measured using CALIBADC if not) then you only need two resistors, say 33k from the negative voltage to an ADC pin and 10k from the ADC pin to the PICaxe supply rail.
If the supply rail resistor is accidentaly disconnected, the ADC will be exposed to negative voltage. Don't you think a protective diode be usefull ?
 

AllyCat

Senior Member
Hi,

I did consider that, but the "static protection" internal diode should keep the PICaxe happy. With the suggested 33k pull-down, the current will be less than 0.5 mA and AFAIK it's a substrate diode which should take the current easily.

The current is probably less than when the "approved" PICaxe Programming circuit is used with a "real" RS232 interface, where the input pin can be pulled down by a similar negative voltage, via only 22k.

Cheers, Alan.
 

Steve2381

Senior Member
Mmm. Well I put the negative input through a in4001, and a 33k to the input pin, and tied that pin to 5v.
According to my multimeter, I get a range of 3.86v to 4.9v depending on the 0v to -12v input. Fine, ok so a range of 1.04v.
I only need accurancy to 8 steps, so I thought we had cracked it.

But... I only seem to get a reading of 808 at the full -12v, or 1023 at 0v. Nothing inbetween? (the multimeter shows the stepped voltages at the picaxe pin however) Suggestions?
 

hippy

Technical Support
Staff member
A rough circuit diagram would probably help clarify exactly what you have.
 

Steve2381

Senior Member
OK. Only think I have to sketch out a circuit diagram is MS Paint... takes ages! Perhaps a pen, paper and camera is a better option.

No idea what is going on. I am getting better results with my LDR and led!
 

BESQUEUT

Senior Member
But... I only seem to get a reading of 808 at the full -12v, or 1023 at 0v. Nothing inbetween? (the multimeter shows the stepped voltages at the picaxe pin however) Suggestions?
May we read you code ?
What are you reading for intermediates voltages ?
 

Steve2381

Senior Member
I am at work now... so can't post the code. Will do that when I get home in the morning (nights... boo)

Intermediate voltages on the input pin - can't remember off the top of my head. Basically, with the input standing at -12.4v ... it showed 3.86v on the multimeter. Then it would step 8 voltages back up to 4.9v when there was no input (0v).
The input source basically appears to step 8 steps between 0v and -12.4v. I only need to read these 8 steps. I want to divide my Readadc(10) input and divide it into a result that tells me which one of the 8 steps I am reading.

But.. as I say, the voltmeter says I am reading 8 steps, but the adc input was only giving me 2 results. I am obviously missing something. Perhaps I need to alter the accuracy of the ADC input
 

Steve2381

Senior Member
Oh... just to clarify the context of this project.
Basically I have a Picaxe input from each of the outputs of a purchased RGB led controller. I have one Red, one Blue and one Green. These inputs step 8 voltages to provide one of 8 levels of brightness in an RGB led. These voltages step from 0v to -12.4v.

My idea (and I stupidly thought it was simple) was to sample each of these 3 inputs and then I could determine the colour being displayed by the purchased RGB controller. It would basically be a mix of the 3x 8 levels of input.

I am starting to think the cheapy RGB controller is sending out something to the RGB strip that the Picaxe doesn't like to read on an ADC pin. I thought it would be a simple voltage.

I did notice that the voltage 'steps' never stayed constant (or anywhere near) on the multimeter. They in fact seem to creep upwards.

Obviously I can read a positive voltage if I read between the 12v of the RGB strip and the negative stepped output. My Picaxe circuit however only sees the negative signals

Never simple is it (well it is if you know what you are doing!)
 

AllyCat

Senior Member
Hi,

It's very probably using PWM to control the brightrness. So your "slow" LDR was a definte advantage. ;)

Therefore, just put a capacitor, probably in the range 100 nF to 10 uF between the PICaxe ADC input pin and ground to give you a "d.c." voltage level.

Note that LEDs have a "constant" voltage drop regardless of brightness; the voltage drop changes only across the (built-in) resistors. Thus the brightness will not be directly proportional to the voltage you measure.

Cheers, Alan.
 

BeanieBots

Moderator
It very unlikely that any RGB controller puts a "voltage" across the LEDs.
At best it is likely to be a PWM drive, at worst, they are 'intelligent' LEDs and are being sent 'data'.
Is it not possible for you to re-arrange your PICAXE supply so that the signal you want to measure is positive with respect to the input?
Failing that, AllyCat's suggestion is the tried and tested method.
As for a catch diode. It is not required due to the built in clamp diodes but if you allow any negative current to get in, then ALL ADC readings will be compromised. If you need to clamp, it MUST be a schottky type diode. A regular type will not clamp close enough to 0v to avoid the effect.
 

Steve2381

Senior Member
Yea its not as simple as I thought it was! Who knew the LDR was the best option.

They are not smart Leds... its not sent data. Simply a varied supply on one of the 3 colour signal wires. So more than likely then its PWM. Could I read that in another way?
I already added the capacitor that Allycat suggested, but it messes up the RGB controller. It loses some of its 'steps'. Possibly better isolation between the RGB controller and the Picaxe is needed.
This could end up being an LDR and a led in a tube at this rate.
 

AllyCat

Senior Member
Hi,

I'm surprised that a capacitor on the far end of a 33k resistor could upset the LED driver, but maybe it's some weird type of self-oscillating or feedback design.

If it is PWM, then another possibility would be for the PICaxe to read the duty cycle (pulse width and period). Not quite s simple as READADC (when it works) but perhaps just two PULSINs (one high and one low) and a little maths.

Cheers, Alan.
 

BeanieBots

Moderator
I agree with AllyCat except I don't think the cap was put at the right end of the 33k!

Sounds like the LED controller is common anode, hence the negative drive.
How about trying to locate the controller 0v and referencing to that.

Alternatively, use a tranny to invert the signal and then either filter and scale or do as Ally suggests and measure the time.
How accurate do you need it?
What is your actual goal?
 

Steve2381

Senior Member
Thanks for the suggestions guys.
Basically I am trying to get an Adafruit RGB led ring to 'match' the output of the 'off-the-shelf' RGB controller.
I can't get to the controller to hack it, so I basically only have its output to play with.

The Adafruit Led ring basically needs the data sent to it in 3 variable (red,green and blue) e.g (255,255,255) would equal white.
So if I can read the 8 steps of each colour, then I should be able to mirror the colours the controller is showing with the RGB ring.

Capacitor was certainly the correct end of the circuit! It does seem a very touchy controller.
PWM... good idea, but I don't think I can read 3x Pulsins at the same time to get the data quick enough.

I will keep tinkering
 

hippy

Technical Support
Staff member
If planning to control an Adafruit RGB LED ring; have you managed to control that from a PICAXE ?

I was under the impression that LED ring used WS2812 controlled LED modules which have a 1-wire interface with timing requirements which prevents it being directly controlled by a PICAXE.
 

Steve2381

Senior Member
Ha... thats my next issue then. I don't want flashy routines or anything - just straight colours. If the Adafruit RGB ring isn't compatible, then I will simply make a ring using some SMT RGB leds I have here. I can easily print off a 3d ring to mount them in,

To be honest. My best results by far are the LDR's simply returning an ADC input. Ony problem is the input varies a little too much to make it accurate.
 

rossko57

Senior Member
PWM... good idea, but I don't think I can read 3x Pulsins at the same time to get the data quick enough.
But you don't need to read at the same time, sampling the channels one at a time from successive 'frames' will do.

To be honest. My best results by far are the LDR's simply returning an ADC input. Ony problem is the input varies a little too much to make it accurate.
Another job for the capacitor, 'converting' pulses to DC
 

premelec

Senior Member
@Steve2381 - APA102 RGBs can be controlled by PICAXE fine - and you can get them by the piece or string. I've also used the 4 PWMOUTs on a 14M2 to control individual R - G - B units easily. the WS2812 has a number of shortcomings - like strobe rate and tight timing to drive them... see other posts..
 

Steve2381

Senior Member
I tried Pulsin.... didn't get any data at all, so I can only assume its not PWM. I did realise that I don't need to sample all three channels at once.

I also tried sampling the ADC input from the LDR 10 times and taking an average. Definitely better... but some of the returned values are still too close together (depending on the stepped input) to be accurate upon conversion.

Its always the stupid little circuits that give me so much hassle
 

grim_reaper

Senior Member
At the risk of repeating my earlier comment - in all seriousness this time - have you got a phototransistor or photodiode you can use instead of the LDR? I believe (from my GCSE work nearly 20 years ago!!) they are far more responsive and might help if you want to continue using your DIY opto-coupler!
 

AllyCat

Senior Member
Hi,

I tried Pulsin.... didn't get any data at all, so I can only assume its not PWM.
No, it's the current which is being switched by PWM, there's no certainty that the voltage is swinging between 0 and -12 volts. In fact it probably isn't, because the LEDs are not a (pure) resistive load. It's very difficult to design a digital level-shifting circuit when you don't know any of the voltage levels! Ideally you need a 'scope, but maybe enough information can be gleaned from a multimeter and perhaps a LED + resistor and/or a peak detector (diode + capacitor).

An analogue input gives much more information, even if it's only 0 or 255/1023. But you haven't told us any of the ADC values you're getting, so analysis is virtually impossible. An LED/photo-diode (or transistor) is also likely to need low-pass filtering, so I'd still like to know why a capacitor via a 33k resistor "messes up the RGB controller".

Cheers, Alan.
 

Goeytex

Senior Member
I have been following this thread but have not commented till now.

It would be helpful if we knew what this off-the-shelf controller was. Is there a part/model number or a link to where it was purchased?

1) Are limit resistors required with this "no-name" controller? If so then where are they placed? What are the values?
2) No diagram has been provided of the "no-name" controller and its wiring/connections to a power source or to the RGB LED's.
3) No diagram has been provided as to how the Picaxe is connected to the no-name RGB controller. Are the supply grounds tied together?
4) No part numbers have been provided for the LED's.
5) I find it doubtful that there is actually a negative voltage present if the Picaxe Supply Ground and controller supply ground are tied together. We need a diagram.

It seems to me that some assumptions are being made that may not be correct. With such vague information provided. those attempting to help can only give a "best guess".
 

Steve2381

Senior Member
Ok guys.

The RGB controller is some horrible Chinese thing with no details - mounted in a chassis and virtually inaccessible. It directly connects to a strip of 5050 RGB leds. This connection consists of R/G/B and a +12v.
It runs off a 12v PSU that came with it.

The Picaxe & controller cannot share a ground, as I have no access to it (hence my opto-coupler idea). Part numbers for the leds? Well all I know is its a strip of 5050 RGB leds.
I emailed the original supplier of the LEDS and they have not responded with any further details.

Multimeter across the output of the controller (and all I have is a multimeter) provides -12.4v when fully illuminated between the +12v common and the coloured LED supplies. So the negative voltage is relative to the common +12v supply for the leds.

Steps from the ADC? These vary depending on what resistors I am trying, but basically the values start a reasonable distance apart when the input is low and get too close together for accurate reading by the time the leds are nearly on full.

Capacitor from the Picaxe ADC pin (after the 33k series resistor)... it made the RGB leds stop dimming after 3 or 4 steps down from full brightness. I have no idea why.

My lack of knowledge isn't helping I know. For instance, I don't understand how a photodiode or phototransistor would give me an ADC input. I understand the principle of the LDR.

Don't worry, I will plod on. I didn't expect it to become a 3 page thread!
 

hippy

Technical Support
Staff member
So between the RGB controller and the PICAXE, beyond the three RGB signals; what other connection do you have to the PICAXE when trying to measure PULSIN ?

And what type of 12V PSU is it for the controller, AC or DC ? If DC then there's a good chance that the 0V is common between PSU and controller, and that can be used as a common 0V to the PICAXE.
 

BESQUEUT

Senior Member
Multimeter across the output of the controller (and all I have is a multimeter) provides -12.4v when fully illuminated between the +12v common and the coloured LED supplies. So the negative voltage is relative to the common +12v supply for the leds.
Connect multimeter negative to PSU negative, and measure RGB...
 

Steve2381

Senior Member
But... if I cannot get to the controller (its been installed in a cabinet in the most ridiculous place), then I cannot really get at the PSU to obtain a negative (its a 2.1mm plug).

I (rightly or wrongly) tried obtaining the PWM using the 33k resistor method. Input through the 333k, and then tied to OV at the Picaxe pin. This gave me positive readings on my multimeter depending on the brightness of the RGB leds.
But... it would not read correctly on the PWM input either. I only got a PWM result of 1023 or 808... no matter what voltage appeared on the multimeter (the multimeter did show 8x steps of voltage)
 

Steve2381

Senior Member
Just so I am not wasting anyones time.... I have it sorted.

I stuck with my 70k-170k LDR idea. I put 3x white leds opposite 3x LDRs in a 3d printed custom holder. Took that in on an ADC input, tied to 0v with a 1k and a 220uf capacitor.
I get consistant (and pretty quick) results with that.

This method at least means I don't have to electrically link to the dodgy Chinese controller.

Thanks all
 

Skiwi

New Member
Back to the original question - If you want to read the -ve voltage with accuracy, forget the LED idea.
Your mention of an op-amp is the best and simplest way to go.
A simple inverting op-amp, e.g.LM741, will give an exact positive voltage, relative to the negative input.
You will need a +/- 12V supply. Ideally +/15V. Most OP-amps wont swing the output voltage to full rail voltage. So you either need a higher supply voltage, or reduce the input voltage with a voltage divider. If you lower your input by half with a voltage divider, to -6V (remembering that the input impedance of the op-amp will be the value of the input resistor), then your output voltage will be +6V
So reduce the input to one third, to say -4V, the output will be +4V and a safe voltage for direct input to the picaxe to READADC
You can use a trim pot somewhere on either the voltage divider, or the feedback resistor to alter the gain of the op-amp, to adjust the value read by the picaxe. Alternatively, use a simple multiplier in the program to adjust to calibrate the reading back to 12V

If you dont have a +/- supply, there are also ICs that create a negative voltage from a positive voltage, e.g.ICL7662. Very easy to use. I use this IC to create -12V, so that I can read +/- voltage across a current shunt, to read +/- current
 

Attachments

Top