Main Site Documentation

NullReferenceException with AnalogInput


So I’ve been playing around with the AnalogInput class and I’ve run into a bug I’m not sure how to deal with.

Here is the setup: I’ve got a 12mm photoresistor and a 15k resistor running off the extender’s 5v into pin 3. I’m reading the values coming into the AnalogInput and it works great, except…

…when I turn the lights out and basically the photoresistor drops to zero resistance. At that point I read 3.3v into the pin, which I expect, but then I get this ugly error:

#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
#### Message: 
#### Microsoft.SPOT.Application::OnEvent [IP: 0098] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####

A first chance exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll
An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll

I dont’ get it in any particular place, other than it happens after I read a 1.0 or 3.3 (using ReadProportional or ReadVoltage). It doesn’t happen when I read the value, it happens after I read it.

I can’t even catch the error, nor is there any disassembly to read. In technical terms, it blows up.

So, yes, I could bump up to a larger resistor and possibly never reach 3.3V+, but considering the inputs are rated to handle 5V, I’m chalking this up to a bug.

Any ideas, thoughts? Am I missing something obvious?


Mr. Bildo


aren’t analog inputs only safe up to 3v3? You can’t put more in it and not expect weirdness including possible physical damage. (that’s certainly the case with the USBizi line so you should check your datasheet)


Interesting, somewhere I read they are “tolerant” up to 5V, but maybe that’s only the digital pins?

Yikes. If that’s truly the case, I never got much over 3.3V, because I tested everything before hooking up the FEZ Spider and I was only getting maybe 3.5V max on my meter. Still…


5v tolerant is definetely on the Digital ports, not the ADC.


I would assume that as the ports are 5V tolerant, even the ADC inputs would not be damaged with up to 5V.

I wouldn’t be surprised to see “undefined behaviour” when reading an ADC input which was over 3.3V.

Of course, these are assumptions and it would be nice if they could be confirmed or disproved.


ADC wouldn’t be damaged at 5V but the readings will be wrong.

Note, Gadgeteer specification allows fro 3.3V only due to some processor not being 5V tolerant like FEZ Hydra. NEVER use 5V with a gadgeteer mainboard. Why? In near future you will order FEZ Hydra and plug your current 5V circuitry init forgetting that it is not 5V tolerant, which is damage your new mainboard :frowning:


@ Gus

Just to clarify, though…if we’re using the USB Client SP or DP modules, those take care of routing the 5v to the right place to supply circuits that require 5v, correct?

So the admonition about 5v is about making sure that we don’t have more than 3v3 after whatever our sensor/circuit/etc. is doing, is that right?


OK, so my assumptions about the 5V tolerance were completely wrong. :slight_smile: Thanks for the clarification.

It makes sense that the readings over 3.3V would be wonky. I’m just still suprised by the particular error that I got, which gave me no clue as to what was actually wrong. I guess I expected to see Hardware error or Voltage overflow or something like that.

In any case, the circuit is fixed. ~3V going into the photoresistor now so there is no chance of it exceeding the 3.3V max.

–Mr. Bildo


I wasn’t talking about power source. That is taken care of.

If you are using hte extender module or making your own module, you must not feed the mainboard with anything above 3.3V (actually 3.6V). This is in the gadgeteer specifications.


Thanks, Gus.

Just trying to make sure I was understanding you correctly. Appreciate the follow-up. :slight_smile:


As a followup to this issue, what is the suggestion on handling the 5V to 3.3V? In my current case I’m just using reistors, but now I’m thiking about digging out a diode out of my parts bin. Actually, maybe it’s best just to buy a few voltage regulators to have on hand?

Thanks again.


@ Mr. Bildo

If you’re using this with the extender module, why not pull power off the 3v3 pin rather than 5v?


@ devhammer – Mostly I was just playing around with variable resistors where I draw down the 5V and get a larger voltage “playingfield” on my board, if you will. Really it was more for experimentation and got me thinking. (which is where I got into trouble :wink: )


You might look like you get a “bigger playing field” but half that field is situated over a chasm that you really don’t want to go near :slight_smile:

My suggestion is know your voltage outputs of things you are hacking and the input tolerances of your devices. I would only ever use 3v3 for a device that I expected to return an analog voltage to my device, that way the maximum it could output is 3v3 (unles said device was a boost converter :))


@ Brett

This is so true, it had to be quoted. :smiley: