I am currently working on a photo-transistor like this: http://www.ebay.com/itm/10pcs-ITR8307-S18-TR8-Optical-Switches-Reflective-Phototransistor-Output-/381374566327?hash=item58cbb20fb7:g:lgoAAOSwr81UQ7KG

It very nicely allows me to identify colors white, green and black. The problem I am having is that the Arduino is powered with batteries so the readings for those colors change as voltage in the batteries reduces.

I tried to make the reading proportional to the voltage in the system, but this still gives different values, I guess the readings change non-linearly as voltage changes.

I wonder what will be the solution to this problem: Specifically, how to make the reading of an IR sensor the same for the same color at different voltages?

This is the code I am using so far:

void setup() {
// initialize serial communication at 9600 bits per second:

void loop() {
// read the input from the IR sensor on pin A0:
float sensorValue = analogRead(A0);
// read the voltage in the system by connecting pin A1 to Vcc, with the same resistor value used in the IR sensor:
float Vref = analogRead(A1);

// adjust reading value to voltage in system:
float AdjustedReading = sensorValue/Vref;

  • 1
    How it it connected? How are you powering you arduino?
    – Gerben
    Dec 8, 2015 at 20:10
  • What is Vref connected to...?
    – Majenko
    Dec 8, 2015 at 20:19
  • the device is powered with 2 aa batteries. The Attemega328 works well at voltages as low as 1.8v. but in the ranges from 3.5v (the maximum with 2 AA batteries) to 1.8 v, for the same color I get different IR readings, which prevents me of setting thresholds to define each color.
    – Camilo
    Dec 8, 2015 at 21:11
  • The circuit is very similar to this: blog.huntgang.com/2014/06/17/arduino-tcrt5000-build-ir-sensor the only difference is the polarity of the IR emitter which has to be inverted for the phototransistor I am using.
    – Camilo
    Dec 8, 2015 at 21:18
  • 2
    That doesn't show what Vref is. Is it by chance connected direct to the Vcc pin of the ATMega? I.e., connected to V+ of the battery? If so it will always read the same value and is of absolutely no use in this situation. Instead you need to back-calculate the Vcc vaue from reading the internal 1.1V band-gap reference voltage.
    – Majenko
    Dec 8, 2015 at 23:12

1 Answer 1


It sounds to me like your system works, but you have trouble if your voltage drops.

First of all here's 2 tips that might solve your problem.

  1. instead of just making 1 reading do 10 and take an average - this reduces noise.

  2. the voltage reference is allready based on the voltage that powers the system so with your adjusted reading you try to accomplish something which has already happened an therefore has the opposite effect and actually increase the error.

see http://tronixstuff.com/2013/12/12/arduino-tutorials-chapter-22-aref-pin/ for a very good explanation.

  1. Add a RC filter to removes random fluctuations in the signal from you photoresistor

see https://electronics.stackexchange.com/questions/17754/where-to-place-a-capacitor-to-smoothen-ir-sensor-reading for a short description and the calculated example.

If however it is correct, and your readings of colors don't follow a linear scale, you would have to deduce that actual curve and apply it in the code - but that might not be easy.

So it would be much more easy to remove that variable by ensuring that the voltage is constant. Most Arduino boards have an 5V onboard voltage regulator that will keep the voltage exactly at 5 Volt when connected to battery via the power Jack!

(USB bypasses this regulator and have a little voltage drop so it is sligthly lower than 5V, and more very prone to fluctations if coming from a AC source affecting your analog read considerably)

The battery connected onboard voltage regulator however requires a higher input to do the regulation so the battery needs to supply atleast 6.2V or higher, in your case you would have to use a voltage source of less than 6.2V if this is the source of your problems.

(the voltage regulator on original arduino boards needs voltage of 1.2V higher than the needed output - clones might differ and require upto 2v more than the needed output)

hope this can help solve the issue


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.