Go Down

Topic: Problem with potentiometer changing phototransistor readings HELP (Read 1 time) previous topic - next topic


Hello all!

I´m a newbie on arduino, just a few basic ideas .

Now, I´m trying to build a carriage to go away and came back on hanging on a wire, I use a RC ESC to control motor speed regulated via potentiometer and i´m trying to implemet a motor turn impluse detector to know what distance carriage is from starting point.
The problem that is killing me is that a get a initial phototransistor reading, for compare, and then if I move potentiometer phototransistor readings change. Now, what do you think can cause this? Bad electonics, bad programing.

I have the code below now but already made many many changes and variations.

Many thanks in advance.

Code: [Select]
#include <Servo.h>
#include <LiquidCrystal.h>
Servo myservo;  // create servo

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// ---------- pins IN OUT ----------------
//int analogPin= 5; //fototransistor analog pin
int relePin = 6; // pin relay
int tempPin = 1; // pin analogico
int potPin = 2;    // analog pin potenciometro
int ledPin = 13;   // pin LED
int pulsoPin = 5; //detector de impulsos
int ledredPin = 7;
int ledgreenPin = 10;

// --------------- variaveis ----------------

int val;       // valor do potenciometro
int valservo; // valor servo (BEC)
int temp; //temperatura
int sentido; //sentido do alvo
int pulso; // estado dos impulsos 1 ou 0
int fotoref; // valor inicial fototransistor
int valfoto; // valor do fototransistor
float distinicial = 3.50; // distancia inicial
float distpulso = 0.12; //distancia percorrida a cada impulso
float distancia = 0;

void setup() {
  lcd.begin(20, 2);
  myservo.write(0); // manda parar servo no arranque
  pinMode(pulsoPin, INPUT);
  pinMode(relePin, OUTPUT);
  pinMode(ledredPin, OUTPUT);
  pinMode(ledgreenPin, OUTPUT);
  fotoref = analogRead(pulsoPin);
  pinMode(tempPin, INPUT);
  distancia = distinicial;
  lcd.print("ALVO A");
  lcd.setCursor (5,1);
  temp = analogRead(tempPin);
  temp = (5.0 * temp * 100.0)/1024.0;
  lcd.setCursor (5,1);
   //temp = temp * 0.48828125;

void loop() {

  // ------------ detector de impulsos ------------

  valfoto = analogRead(pulsoPin);
  Serial.print(" foto ");

  if (valfoto >= fotoref) {

    pulso= 1;

  // ------------- movimento -------------

  val = analogRead(potPin);
  if (val > 590 || val > 640);
    digitalWrite(ledgreenPin, LOW);
    digitalWrite(ledredPin, LOW);

  // ------------- movimento motor avanço ---------
  if (val <= 590 && pulso == 1 ){
    valservo = map(val,0 ,590 ,95 ,50);
    digitalWrite(relePin, LOW);
   digitalWrite(ledgreenPin, HIGH);
    digitalWrite(ledredPin, LOW);



    if (valfoto < fotoref-15){
      distancia = distancia - distpulso;

    //-------------movimento do motor recuo ----------
    if (val >=640 && pulso == 1 ){
      valservo=map(val,640 ,1023 ,50 ,95);
      digitalWrite(relePin, HIGH);
      digitalWrite(ledgreenPin, LOW);
      digitalWrite(ledredPin, HIGH);

      if (valfoto < fotoref-15){
        distancia = distancia + distpulso;


  /*if (val > 590 || val > 640);
    digitalWrite(ledgreenPin, LOW);
    digitalWrite(ledredPin, LOW);



Here I drawled you up a the idea way to hook up a CDS  photo-resistor

Now the photo-transistor is a "on" or off switch so are you using a potentiometer
to set the beam output like the drawing showing how to use a photo-transistor


Dec 09, 2012, 06:04 pm Last Edit: Dec 09, 2012, 06:08 pm by astrostar Reason: 1
Thank you for your quick answer.

My pot is connected like this:

(+ )----->|---(A2)---|<----- (-)

I´ll modify it and add the resistors.

PS. another thing is to connect phototransistor to a digital pin I belive!?


You can read it as a switch  which is the best way to use them. The output is hooked up to a digital pin.


On the logic.png you have a resistor symbol inside a circle... what does it mean? probably is a silly question...

Many thanks for the help :)


I drawled how to use both a photo- transistor top drawing.

And a photo- resistor bottom drawing

The photo-resistor let's you read the light by voltage could be used to slow the motor down as you get close to your stopping point.

Where as the top picture the photo-transistor is more for a on and off state.


I´ve tried the logic.png but no success, changed other things and I cant see state change from HIGH to LOW ever.

at the moment just need a project to detect pulses from a phototransistor capable of made something happend and that remain stable when we turn potentiometer.

Many thanks.

this is driving me crazy ....  :(


If your outside in the day light a photo-transistor not going to work very good.

Go Up