Sensor's values barely change?

I have been stumped for hours now, not sure if my Arduino Uno is broken or something. For the photoresistor, when I put my hand over the resistor, the value barely changes, I have tried many different valued resistors. I am also using a line-follower to detect if there is an object in the way, which also barely changes values. Basically, I'm really really confused at this point and would like any help. I have uploaded the code and a wiring diagram.

#include <Servo.h>
Servo myservo;
const byte Button = 3;
const byte Sensor = 5;
const byte Traffic_R = 9;
const byte Traffic_G = 10;
const byte Traffic_B = 11;
const byte Follower = A5;
const byte ServoPin = A4;
enum vtlStates {TrafficState1, TrafficState2, TrafficState3, TrafficState4, TrafficState5, TrafficState6, TrafficState7};
enum vpStates {pState1, pState2, pState3, pState4};
// enum assigns values to each state (starting from value 0)
vtlStates tlState;  //create a variable tlState
vpStates pState;  //create a variable pState

unsigned long SetupLight, Brightness, SetupFollower, CountStart, CountElapsed, tl4Start, tl4Elapsed, Wait6Start, Wait6Elapsed, tl6Start, tl6Elapsed, tl7Start, tl7Elapsed ;
int ButtonState = 0;
int LoopState = 0;
int LoopLight = 0;
int LoopFollower = 0;
int CountFollower = 0;
int BoomGateState = 0;
int Count = 0;
int CountDelay = 100;
int tl4Delay = 2000;
int Wait6Delay = 1000;
int Wait6Count = 0;
int tl6Delay = 3000;
int tl7Delay = 1000;

void setup() {
  Serial.begin(9600);
  myservo.attach(ServoPin);
  pinMode(Button, INPUT);
  pinMode(Sensor, INPUT);
  pinMode(Traffic_R, OUTPUT);
  pinMode(Traffic_G, OUTPUT);
  pinMode(Traffic_B, OUTPUT);
  pinMode(Follower, INPUT);
  pinMode(ServoPin, OUTPUT);
  tlState = TrafficState1;
  SetupLight = analogRead(Sensor);
  SetupFollower = analogRead(Follower);
  myservo.write(0);
}

void loop() {
  bool ButtonStateOpp = digitalRead(Button);
  LoopLight = analogRead(Sensor);
  LoopFollower = analogRead(Follower);

  if (ButtonStateOpp == LOW) {
    ButtonState = -~ButtonStateOpp;
  }

  if (LoopLight < SetupLight - 50) {
    LoopState = 1;
    Brightness = 128; // Night
  }
  else {
    LoopState = 0;
    Brightness = 0; // Day (255 is off)
  }

  if (Wait6Count == 0) {
    Wait6Start = millis();
  }
  if (ButtonState == 1 && tlState == TrafficState1) {
    analogWrite(Traffic_R, Brightness);
    if (Wait6Count == 0) {
      Wait6Count++;
    }
    Wait6Elapsed = millis() - Wait6Start;
    if (Wait6Elapsed > Wait6Delay) {
      tl6Start = millis();
      Wait6Count = 0;
      tlState = TrafficState6;
    }
  }


//  Serial.print("A5 - ");
//  Serial.println(analogRead(A5));
//  Serial.print("A4 - ");
//  Serial.println(analogRead(A4));
//  Serial.print("A3 - ");
//  Serial.println(analogRead(A3));
//  Serial.print("A2 - ");
//  Serial.println(analogRead(A2));
//  Serial.print("A1 - ");
//  Serial.println(analogRead(A1));
//  Serial.print("A0 - ");
//  Serial.println(analogRead(A0));
  Serial.print("SetupL  - ");
  Serial.println(SetupLight);
  Serial.print("LoopL   - ");
  Serial.println(LoopLight);
//  Serial.print("milli  - ");
//  Serial.println(millis());
//  Serial.print("Start  - ");
//  Serial.println(Wait6Start);
//  Serial.print("Wait6  - ");
//  Serial.println(Wait6Count);
//  Serial.print("SetupF - ");
//  Serial.println(SetupFollower);
//  Serial.print("LoopF  - ");
//  Serial.println(LoopFollower);
//  Serial.print("Gate   - ");
//  Serial.println(BoomGateState);
//  Serial.print("Servo  - ");
//  Serial.println(myservo.read());
//  Serial.print("State  - ");
//  Serial.println(tlState + 1);
//  Serial.print("Button - ");
//  Serial.println(ButtonState);
  Serial.println(" ");

  switch (tlState)  {
    case TrafficState1: {
        myservo.write(0);
        analogWrite(Traffic_R, Brightness);
        analogWrite(Traffic_G, 255);
        analogWrite(Traffic_B, 255);

        if ((myservo.read() == 0 && ButtonState == 0) && (SetupFollower + 10 < LoopFollower || LoopFollower < SetupFollower - 10)) {
          tlState = TrafficState2;
        }
      }
      break;

    case TrafficState2: {
        if (Count % 2) {
          analogWrite(Traffic_R, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_R, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        if (myservo.read() != 90) {
          myservo.write(myservo.read() + 1);
        }
        if (myservo.read() == 90) {
          BoomGateState = 1;
          tlState = TrafficState3;
        }
      }
      break;

    case TrafficState3: {
        analogWrite(Traffic_R, 255);
        analogWrite(Traffic_G, Brightness);
        analogWrite(Traffic_B, 255);

        if (BoomGateState = 1 && (SetupFollower + 5 < LoopFollower || LoopFollower < SetupFollower - 5)) {
          tl4Start = millis();
          tlState = TrafficState4;
        }
      }
      break;

    case TrafficState4: {
        if (Count % 2) {
          analogWrite(Traffic_G, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_G, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        tl4Elapsed = millis() - tl4Start;
        if (tl4Elapsed > tl4Delay) {
          tl4Start = millis();
          tlState = TrafficState5;
        }
      }
      break;

    case TrafficState5: {
        analogWrite(Traffic_G, 255);

        if (Count % 2) {
          analogWrite(Traffic_R, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_R, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        if (myservo.read() != 0) {
          myservo.write(myservo.read() - 1);
        }
        if (myservo.read() == 0 && ButtonState == 1) {
          BoomGateState = 0;
          if (Wait6Count == 0) {
            Wait6Count++;
          }
          Wait6Elapsed = millis() - Wait6Start;
          if (Wait6Elapsed > Wait6Delay) {
            tl6Start = millis();
            Wait6Count = 0;
            tlState = TrafficState6;
          }
        }
        else if (myservo.read() == 0 && ButtonState == 0) {
          BoomGateState = 0;
          SetupFollower = analogRead(Follower);
          ButtonState = 0;
          tlState = TrafficState1;
        }
      }
      break;

    case TrafficState6: {
        analogWrite(Traffic_R, 255);
        analogWrite(Traffic_B, Brightness);

        tl6Elapsed = millis() - tl6Start;
        if (tl6Elapsed > tl6Delay) {
          tl7Start = millis();
          tlState = TrafficState7;
        }
      }
      break;

    case TrafficState7: {
        if (Count % 2) {
          analogWrite(Traffic_B, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_B, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        tl7Elapsed = millis() - tl7Start;
        if (tl7Elapsed > tl7Delay) {
          SetupFollower = analogRead(Follower);
          ButtonState = 0;
          tlState = TrafficState1;
        }
        break;
      }
  }
}

You appear, based on what I see in that Fritzing diagram, to have a light dependent resistor (LDR) but it is not correctly connected. The LDR should be part of a voltage divider and the analog pin should be connected to the middle point of this divider.

Ok, thanks for the reply. I think I have done what you have mentioned, but it still won't change the value by any more than +/- 5.

A) You appear to have connected the LDR to digital pin 6. You must use an analog pin, say A0.
B) you must use analogRead() to read it.

Show the code you are using.

I put the code in the original message under the image, also I think pin 6 on the uno is an analog pin and can analog read.

Pin 6 can be be used for analogWrite(), therefore it refered to as an "analog pin". You can't, however, use it for analogRead() which you are attempting to do when you read an LDR.

Oh didn't know that, thanks. I'll try that now, but still the line-follower has the same issue, do I need a resistor or something there? I just followed everything I saw online and not sure if it's a hardware issue or not.

What component is that "line follower" ? Please link to a product page where it is described or sold.

This is it: KY-033 Line Tracking Sensor Arduino circuit and programming.

OK. I didn't recognise it on your Fritzing diagram.
In the link you supplied, there is a simple test schematic and sketch to try. Get it working stand alone before trying to integrate it into your main sketch

The "line follower" sensor is digital. You get HIGH if enough IR is reflected back to trigger the sensor and LOW if not enough IR is reflected back to trigger the sensor. The knob adjusts the trigger point.

Hi, I tried this and changed around the code so it's all digital, but still the value doesn't change from 0 when I put white paper in front of it. I am starting to think the sensor is faulty, is there anything wrong with my code? Would really appreciate any reply!

#include <Servo.h>
Servo myservo;
const byte Button = 2;
const byte Traffic_R = 3;
const byte Traffic_G = 5;
const byte Traffic_B = 6;
const byte Follower = A5;
const byte ServoPin = A4;
const byte Sensor = A3;
enum vtlStates {TrafficState1, TrafficState2, TrafficState3, TrafficState4, TrafficState5, TrafficState6, TrafficState7};
enum vpStates {pState1, pState2, pState3, pState4};
// enum assigns values to each state (starting from value 0)
vtlStates tlState;  //create a variable tlState
vpStates pState;  //create a variable pState

unsigned long SetupLight, Brightness, FollowerVal, CountStart, CountElapsed, tl4Start, tl4Elapsed, Wait6Start, Wait6Elapsed, tl6Start, tl6Elapsed, tl7Start, tl7Elapsed ;
int ButtonState = 0;
int LoopState = 0;
int LoopLight = 0;
int CountFollower = 0;
int BoomGateState = 0;
int Count = 0;
int CountDelay = 100;
int tl4Delay = 2000;
int Wait6Delay = 1000;
int Wait6Count = 0;
int tl6Delay = 3000;
int tl7Delay = 1000;

void setup() {
  Serial.begin(9600);
  myservo.attach(ServoPin);
  pinMode(Button, INPUT);
  pinMode(Sensor, INPUT);
  pinMode(Traffic_R, OUTPUT);
  pinMode(Traffic_G, OUTPUT);
  pinMode(Traffic_B, OUTPUT);
  pinMode(Follower, INPUT);
  pinMode(ServoPin, OUTPUT);
  tlState = TrafficState1;
  SetupLight = analogRead(Sensor);
//  SetupFollower = digitalRead(Follower);
  myservo.write(0);
}

void loop() {
  bool ButtonStateOpp = digitalRead(Button);
  LoopLight = analogRead(Sensor);
//  LoopFollower = digitalRead(Follower);
  FollowerVal = digitalRead(Follower);

  if (ButtonStateOpp == LOW) {
    ButtonState = -~ButtonStateOpp;
  }

  if (LoopLight < SetupLight - 50) {
    LoopState = 1;
    Brightness = 128; // Night
  }
  else {
    LoopState = 0;
    Brightness = 0; // Day (255 is off)
  }

  if (Wait6Count == 0) {
    Wait6Start = millis();
  }
  if (ButtonState == 1 && tlState == TrafficState1) {
    analogWrite(Traffic_R, Brightness);
    if (Wait6Count == 0) {
      Wait6Count++;
    }
    Wait6Elapsed = millis() - Wait6Start;
    if (Wait6Elapsed > Wait6Delay) {
      tl6Start = millis();
      Wait6Count = 0;
      tlState = TrafficState6;
    }
  }


//  Serial.print("A5 - ");
//  Serial.println(analogRead(A5));
//  Serial.print("A4 - ");
//  Serial.println(analogRead(A4));
//  Serial.print("A3 - ");
//  Serial.println(analogRead(A3));
//  Serial.print("A2 - ");
//  Serial.println(analogRead(A2));
//  Serial.print("A1 - ");
//  Serial.println(analogRead(A1));
//  Serial.print("A0 - ");
//  Serial.println(analogRead(A0));
//  Serial.print("SetupL  - ");
//  Serial.println(SetupLight);
//  Serial.print("LoopL   - ");
//  Serial.println(LoopLight);
//  Serial.print("Bright  - ");
//  Serial.println(Brightness);
//  Serial.print("milli  - ");
//  Serial.println(millis());
//  Serial.print("Start  - ");
//  Serial.println(Wait6Start);
//  Serial.print("Wait6  - ");
//  Serial.println(Wait6Count);
  Serial.print("Follower - ");
  Serial.println(FollowerVal);
//  Serial.print("Gate   - ");
//  Serial.println(BoomGateState);
//  Serial.print("Servo  - ");
//  Serial.println(myservo.read());
//  Serial.print("State  - ");
//  Serial.println(tlState + 1);
//  Serial.print("Button - ");
//  Serial.println(ButtonState);
  Serial.println(" ");

  switch (tlState)  {
    case TrafficState1: {
        myservo.write(0);
        analogWrite(Traffic_R, Brightness);
        analogWrite(Traffic_G, 255);
        analogWrite(Traffic_B, 255);

        if ((myservo.read() == 0 && ButtonState == 0) && FollowerVal == 1) {
          tlState = TrafficState2;
        }
      }
      break;

    case TrafficState2: {
        if (Count % 2) {
          analogWrite(Traffic_R, 0);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_R, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        if (myservo.read() != 90) {
          myservo.write(myservo.read() + 1);
        }
        if (myservo.read() == 90) {
          BoomGateState = 1;
          tlState = TrafficState3;
        }
      }
      break;

    case TrafficState3: {
        analogWrite(Traffic_R, 255);
        analogWrite(Traffic_G, Brightness);
        analogWrite(Traffic_B, 255);

        if (BoomGateState = 1 && FollowerVal == 1) {
          tl4Start = millis();
          tlState = TrafficState4;
        }
      }
      break;

    case TrafficState4: {
        if (Count % 2) {
          analogWrite(Traffic_G, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_G, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        tl4Elapsed = millis() - tl4Start;
        if (tl4Elapsed > tl4Delay) {
          tl4Start = millis();
          tlState = TrafficState5;
        }
      }
      break;

    case TrafficState5: {
        analogWrite(Traffic_G, 255);

        if (Count % 2) {
          analogWrite(Traffic_R, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_R, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        if (myservo.read() != 0) {
          myservo.write(myservo.read() - 1);
        }
        if (myservo.read() == 0 && ButtonState == 1) {
          BoomGateState = 0;
          if (Wait6Count == 0) {
            Wait6Count++;
          }
          Wait6Elapsed = millis() - Wait6Start;
          if (Wait6Elapsed > Wait6Delay) {
            tl6Start = millis();
            Wait6Count = 0;
            tlState = TrafficState6;
          }
        }
        else if (myservo.read() == 0 && ButtonState == 0) {
          BoomGateState = 0;
//          SetupFollower = digitalRead(Follower);
          ButtonState = 0;
          tlState = TrafficState1;
        }
      }
      break;

    case TrafficState6: {
        analogWrite(Traffic_R, 255);
        analogWrite(Traffic_B, Brightness);

        tl6Elapsed = millis() - tl6Start;
        if (tl6Elapsed > tl6Delay) {
          tl7Start = millis();
          tlState = TrafficState7;
        }
      }
      break;

    case TrafficState7: {
        if (Count % 2) {
          analogWrite(Traffic_B, Brightness);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }
        else {
          analogWrite(Traffic_B, 255);
          CountElapsed = millis() - CountStart;
          if (CountElapsed > CountDelay) {
            CountStart = millis();
            Count++;
          }
        }

        tl7Elapsed = millis() - tl7Start;
        if (tl7Elapsed > tl7Delay) {
//          SetupFollower = digitalRead(Follower);
          ButtonState = 0;
          tlState = TrafficState1;
        }
        break;
      }
  }
}

@6v6gt gave a suggestion in post #10. Did you follow that?

Hi,
As @6v6gt has suggested in post #10, leave your current sketch.

Write a sketch that JUST reads the sensors, forget motor control and the other stuff.

JUST WRITE CODE that has your sensors and sends data to the IDE monitor.

A picture of your project would also help.

Do you have a DMM?

Tom.... :smiley: :+1: :coffee: :australia:

Thank you all for the replies! I followed another guide exactly, Using the Tracking Sensor Module KY-033 with Arduino - Phipps Electronics. Still, it only comes up as "Light Surface", never "Dark Surface" (I think cause the sensor is active low).

Hi,
Have you tried adjusting the pot on the sensor board?

As you bring your hand closer to the pair, the output should change.

Tom.. :smiley: :+1: :coffee: :australia:

Hi,
Can you make this circuit;

Then run this code, look in the IDE serial monitor set at 9600baud.


byte sensorPin = 3;
bool sensorOutput;


void setup()
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop()
{
  sensorOutput = digitalRead(sensorPin);
  if (sensorOutput == HIGH)
  {
    Serial.println("Sensor Output HIGH");
    
  }
  else
  {
    Serial.println("Sensor Output LOW");
  }
}

Then move your hand towards and away from the sensor.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Hey,
I really appreciate all the effort so far, just now as I double checked your diagram, I had the output pin and voltage pin swapped :frowning: . I just blindly followed what was done in my tutorial class diagram, not realising the parts were different. Sorry, I feel like the biggest idiot.

sus, probably a faulty board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.