0

I'm using an HC-SRO4 Ultrasonic Sensor and a D10K Potentiometer. So, I was just messing around with the potentiometer in the program and then tried to change the brightness of the LED which turns on if a certain condition is met, but the potentiometer acts like an ON/OFF switch instead. I'd like to know why this happens and how can I change the LED's brightness normally in this situation.

long duration;
int distance;
int trigPin = 9;
int echoPin = 10;
int buzzer = 11;
int LED = 3;
boolean detected = false;
#define pot 0
int val = 0;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  pinMode(buzzer,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
val  = analogRead(pot);
val  = map(val, 0, 1023, 0, 255);
Serial.println  (distance);

if(distance < 50 && distance != 0){ // distance != 0  because sometimes the distance's first value is sometimes 0 which ruins the purpose of
 // the program
  analogWrite(LED, val);
  alarm();
  detected = true; 
}
else if(detected){ 
  digitalWrite(LED,LOW);
  alarm();
}
}

void alarm(){
  tone(buzzer, 450);
  delay(100);
  tone(buzzer, 1000);
  delay(100);
  }
  • 1
    Write a sketch with pot and led only. – Juraj Aug 16 '18 at 17:30
  • Well, I did and it works fine. I can change the brightness of the LED. But what I don't understand is why it doesn't do so in the code above. @Juraj – Arman Samma Aug 16 '18 at 17:46
  • because the analogWrite is conditional to distance sensor reading – Juraj Aug 16 '18 at 17:47
  • Thing is can't I change the brightness while that condition is true? Well, when I make the condition true, (putting some object in front of the sensor) I turn the potentiometer and it still acts like an ON/OFF switch. – Arman Samma Aug 16 '18 at 17:52
7

The answer is very simple. You use pin 12 for LED. Pin 12 can't do analogWrite(). This pin doesn't support PWM on Arduino UNO (Atmega 328)

Pin 3 is not OK either. Use of tone() function disables PWM on pin 3. From reference: "Use of the tone() function will interfere with PWM output on pins 3 and 11".

  • I'm sorry. I had changed my LED pin to 12 (I'll edit it to 3 now) because I was messing around with it. It was originally a PWM pin when this problem happened – Arman Samma Aug 16 '18 at 17:20
  • @ArmanSamma: Please make sure that whatever code you post is always relevant to the question. – Edgar Bonet Aug 16 '18 at 17:32
  • I don't understand. How and why does the tone() function disable PWM on pin 3? @Juraj – Arman Samma Aug 16 '18 at 20:28
  • "tone()" in answer is a link. Read the reference of the tone function. – Juraj Aug 17 '18 at 4:28

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.