0
\$\begingroup\$

I am attempting to create a circuit that activates a DC motor when the photoresistor detects light, and I used tinkercad to simulate it. It worked in tinkercad, but when I tried to create it in person, it didn't seem to work. When I check the photoresistor values, it constantly reads 0 even when the light is on. The transistor is used as an amplifier. I have attached a photo of the tinkercad circuit and the video of my real life circuit.

int analogValue;
int voltage;
int timer = millis();
void setup()
{
  pinMode(A0, INPUT);
  pinMode(7, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  analogValue = analogRead(A0);
  analogWrite(7, voltage);
  if(analogValue > 200){
    if((millis() - timer) > 5000){
    digitalWrite(7, HIGH);
    }
  }
      else{
        timer = millis();
       digitalWrite(7, LOW);
      }
  Serial.print("Photoresistor Value: ");
  Serial.println(analogValue);
  delay(1000);
}

Below is a video and photo on the circuit I created:
https://youtu.be/6rysF_x5tOM

wiring diagram

If anyone could help me figure out why it does not work, I would greatly appreciate it! Thank you.

\$\endgroup\$
3
  • \$\begingroup\$ Try to separate the problem into pieces, eg, remove the motor and its perhaps ill-chosen transistor and debug the photoresistor only to the serial monitor output. Try changing the value of the resistor opposing the photoresistor. Try swapping the positions of the photresistor and fixed resistor. Try substituting a potentiometer for the photoresistor and fixed resistor and running the IDE's canned analog print serial or whatever they call it example. \$\endgroup\$ – Chris Stratton Dec 2 '20 at 2:34
  • \$\begingroup\$ Thank you! It's weird, but I have figured it out. Using a voltmeter, I found that the LDR was not receiving any voltage, so I moved my jumper cables near where it was placed, and it started detecting values. As for the motor, I had to connect the ground to emitter instead of the collector. Instead of a transistor what would you recommend me using? I bought a relay, but I need to get male to female jumper wires. \$\endgroup\$ – Johnny Huang Dec 2 '20 at 3:27
  • \$\begingroup\$ You should probably post a self-answer explaining that it was just a flaky breadboard connections, but congrats for getting it working. As for the transistor, some transistors could work but little TO-92 ones as pictured are a bit suspect; you probably also don't want to use the Arduino to provide motor power. A logic-level FET would be best, though it can be tricky to find a through hole one with a gate threshold (barely on) voltage sufficiently low to be really on at 5 volts. If you look on the Adafruit site they'll carry something, you can get the same part number from any vendor. \$\endgroup\$ – Chris Stratton Dec 2 '20 at 3:36
3
\$\begingroup\$

Using a voltmeter, I found that the LDR was not receiving any voltage, so I moved my jumper cables near where it was placed, and it started detecting values. As for the motor, I had to connect the ground to emitter instead of the collector.

\$\endgroup\$
1
  • \$\begingroup\$ Congratulations on getting it working. Sometimes the learning experience is indeed that translating a conceptual design to a physical implementation is a point where things can go wrong. And breadboards can be flaky, either due to low cost=quality, or less careful usage. \$\endgroup\$ – Chris Stratton Dec 2 '20 at 4:43
0
\$\begingroup\$

If you have it, a multimeter would be great to make sure your circuit it working.

Regardless, remove the pinMode(A0, INPUT) as I believe that sets the pin to digital input, judging from the documentation. See the analogRead example: https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ Also pinMode: https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/

Edit: As mentioned below this may be incorrect. I would still have a multimeter there if possible to check if your circuit is operating correctly. There is really nothing better than having a sanity check that the arduino is actually getting the correct voltage for conversion.

\$\endgroup\$
6
  • \$\begingroup\$ This is not true. Nor do your links do not support such an idea - in fact they contradict it, albeit a bit subtly. But the simple fact is, pinMode(A0, INPUT) is fully compatible with analogRead(A0) - generally it's unecessary but entirely safe, though if the pin had previously been an output setting it in a non-pullup input mode could be desirable as the pullup may remain active and distort the reading. (Distort, not prevent - and it would distort up, not down) \$\endgroup\$ – Chris Stratton Dec 2 '20 at 2:49
  • \$\begingroup\$ ? The example code shows it working without the pinMode. pinMode documentation talks about it being used to turn analog inputs into digital inputs. \$\endgroup\$ – Fratink Dec 2 '20 at 3:11
  • \$\begingroup\$ Those are not incompatible on this platform. Setting an analog pin to input mode is not required (unless it was previously an output) but it also does not cause any harm - all it really does is repeat what was the state-on-reset anyway. So this is not the answer to the asker's issue. Try it yourself if you like, grab an arduino and potentiomater, load up the Analog Read Serial example, then try it with an optional pinMode(xx, INPUT) - you won't see any difference in behavior, because you're only setting the DDRx bit to the exact same value it was initialized to at reset. \$\endgroup\$ – Chris Stratton Dec 2 '20 at 3:13
  • \$\begingroup\$ Interesting, that feature doesn't seem obvious from the documentation. So when you call digitalRead it's either doing an analog read then digital conversion, or behind the scenes setting the hardware state of the pin? \$\endgroup\$ – Fratink Dec 2 '20 at 3:17
  • \$\begingroup\$ A digital read of an analog pin would merely read the state of the appropriate PINC register bit. An analog read configures the ADC mux to sample that pin. This doesn't appear to be like some other chips where (regardless of actual intent) you can configure a GPIO in "analog mode" which disables the digital input totem pole that can tend to consume excessive current at intermediate voltages where both upper and lower input FET's are partially on, causing a wasteful flow of current from the supply through both to ground. Now where's my ATmega data sheet? \$\endgroup\$ – Chris Stratton Dec 2 '20 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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