Fixing wiring errors

Hello! I am new to this, and I was told to create a circuit with a photoresistor, a motor (fan attached), the LED light, and a push button. The goal of my circuit is for the LED to turn on when the room is dark, and the LED to turn off when the room is bright. The motor is supposed to turn off if the LED is on. However, the fan just keeps turning on and off and the LED won't do anything. Can anyone proofread my code and look at my circuit? Thank you!
P.S. I've also tried changing the sensor value to 20, 255, 200, and 500, and none of them worked. All the resistors on the circuit are 220



Can you please post a proper schematic, and post your full code in code tags.

Then we can begin to help you.

I don't know what either of those are, so I tried looking them up and that looks a bit too complex for my level. I can only give you the tinkercad circuit and the block code. Sorry!

You do an analogWrite to pin 9 immediately followed by a digitalWrite.
What do you really want with whatever you have connected up to that pin?

Driving motors from an output pin is not a good idea. You need a mosfet to drive the motor. Also a diode should be placed parralel to the motor.

What value is your ldr...
Usually you use a similar value in series.220 ohm is pretty low...

Perfect!

Can you post the code you have in the IDE?

Just copy and paste this rather than a screenshot:

Block code isn't overly useful as most of us don't use that

That was automatically generated when i downloaded the code from tinkercad. I just want the LED to turn on when its dark out and the LED to turn off when the brightness is too high. What should I put instead?

What does a diode look like? And would that explain the turning on/off repeatedly?

Do i switch the resistor to a higher value? As for the ldr value - it is the one that comes in the arduino uno starter kit

Without the diode, the motor's reverse voltage spikes may damage your board or cause power issues.
Power drops will lead to a restart of your program. And that might explain the strange behaviour. At the end of setup you can ad Serial.println("end of setup");
If that line apears on your monitor more than once, you kniw you have a power issue.
First remove the motor and see if the LED works as expected..

2 Likes

A higher valuemight be better. But in your code you have put the level where you switch the motor also low... that might correct for the non ideal resistor value...
But I would expect around 10 kohm..
Brown black orange or brown black black red...
Can you measure the ldr?
What sensorvalue does your code print to serial?

This

const int buttonPin = 2;         // the number of the pushbutton pin
const int inputPin = A0;         // the number of the analogue input pin
const int randomPin = 9;         // the number of the random pin
const int unknownPin = 13;       // the number of the unknown pin

int sensorValue = 0;             // variable to store the analog sensor reading

int buttonState = 0;             // variable to store the state of the button

void setup() {
  Serial.begin(9600);            // initialize serial communication at 9600 baud rate
  pinMode(buttonPin, INPUT);      // set the button pin as an input
  pinMode(inputPin, INPUT);       // set the analog input pin as an input
  pinMode(randomPin, OUTPUT);     // set the random pin as an output
  pinMode(unknownPin, OUTPUT);    // set the unknown pin as an output
}

void loop() {
  buttonState = digitalRead(buttonPin);     // read the state of the button
  Serial.println(buttonState);              // print the button state (1 for pressed, 0 for not pressed)

  sensorValue = analogRead(inputPin);       // read the analog sensor value from inputPin
  Serial.println(sensorValue);              // print the sensor value to the serial monitor

  analogWrite(randomPin, map(sensorValue, 0, 1024, 0, 255)); // map the sensor value (0-1024) to a range suitable for PWM (0-255) and write it to randomPin

  // If the sensor value exceeds 255 or if the button is pressed, execute the following
  if (sensorValue > 255 || buttonState == 1) {
    digitalWrite(randomPin, HIGH);          // forcefully set the randomPin HIGH, overriding the previous analogWrite (even though it was just set via PWM?? What is the point of that?? Why override the previous analogWrite?)
    digitalWrite(unknownPin, LOW);          // set unknownPin LOW
  } else {
    digitalWrite(randomPin, LOW);           // forcefully set randomPin LOW, overriding the previous analogWrite (even though it was just set via PWM?? What is the point of that?? why override the previous analogWrite?)
    digitalWrite(unknownPin, HIGH);         // set unknownPin HIGH
  }

  delay(10);                                // brief delay - Why? Is this for button debouncing, or for the LED??
}

I took the liberty of changing your code a bit to be a bit cleaner and make more sense, and I've written some comments I'd like answers on.

1 Like

A diode is often a black cylinder with two wires. It has a grey band on one side. That side should point to plus in your setup.
It may have code printed on it.
1N4xxx would be fine.

1 Like

You’ve already had generous help, on very basic stuff, but are you really incapable of answering that yourself?

Thank you so much! The delay was also automatically generated, I have to do some more research on my part.

Yes. I am very new to this, so what may seem easy to you is very new to me.

pro tip: google is your friend. www.google.com

google diode
select images

a7

1 Like

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