Light controlled fan doesn't work!

Hi!

I’m trying to let a Grove Light Sensor v1.2 controle a Grove Mini Fan. But for some reason I can’t get it working, I’m already trying different stuff for days. (yes, i’m quite the beginner.)
Unfortunately I couldn’t find anyone who did something similar.
This is the code that seemed most promising to me, but isn’t working.

int motorPin = 7;
int lightPin = A0;

void setup(){
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}
void loop() {

if(lightPin <= 80){
  int motorValue = 0;
  analogWrite(motorPin, motorValue);
   delay(30);
  
}

if(lightPin >= 80){
  int motorValue = 255; 
 analogWrite(motorPin, motorValue);
  delay(30);
  
}

 
}

It would be ideal if I could also change the speed of the fan, but I don’t think thats possible.
Please! I really hope someone can help me! Thank you!!

You are not reading a value from your lightpin - you will need to use analogRead.

You are trying to use analogWrite on a pin that likely does not support it - try pin 6 or check which of the pins on your Arduino model are appropriate.

Thank you for your quick response!
I’m using a base shield from seeed studio and from what I read they recommend pin 7, but I tried to use pin 6 either way.

I’m not sure if I put analogRead on the right spot, or do I need to do it instead of analogWrite? It’s still not working…

int motorPin = 6;
int lightPin = A0;

void setup(){
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}
void loop() {

 analogRead(lightPin);
 
if(lightPin <= 80){
  int motorValue = 0;
  analogWrite(motorPin, motorValue);
   delay(30);
  
}
analogRead(lightPin);

if(lightPin >= 80){
  int motorValue = 255; 
 analogWrite(motorPin, motorValue);
  delay(30);
  
}

 
}

To use analogRead, you need to put the value it returns in a variable. That variable is what you should be checking in your if statement, not lightpin itself.

Check the docs here: analogRead() - Arduino Reference

Thanks again, I think I slowly start to understand it. I did some changes that (at least that’s what I thought) should solve the problem, but still my fan won’t move. I did manage to get the info from my light sensor! Is there still something I’m doing wrong? Sorry…

int motorPin = 6;
int lightPin = A0;
int analogValue = 0;
int val = 0;

void setup(){
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
 Serial.begin(9600);

}
void loop() {
 
if(analogValue < 180){
  analogRead(analogValue);
  int motorValue = 0;
  analogWrite(motorPin, motorValue);
   delay(30);
  
}

if(analogValue > 180){
  analogRead(analogValue);
  int motorValue = 255; 
 analogWrite(motorPin, motorValue);
  delay(30);
  
}
val = analogRead(analogValue);   
  Serial.println(val);
  delay(500);
 
}

Try this:

int motorPin = 6;
int lightPin = A0;
int val = 0;

void setup()
{
  pinMode(motorPin, OUTPUT);
  pinMode(lightPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  val = analogRead(lightPin);
  Serial.println(val);

  if (val < 180)
  {
    analogWrite(motorPin, 0);
  }
  else 
  {
    analogWrite(motorPin, 255);
  }
  delay(500);
}

Compiled, not tested.

Yes!! It works perfectly
Thank you so much!!