can't use PWM in Arduino while ultrasonic sensor is plugged in.

void setup()
unsigned long time;
{
   pinMode(6, OUTPUT); //trigger
   pinMode(7, INPUT);   // echo
   pinMode(8, OUTPUT); // led
   analogWrite(8, 100); //pwm
}

void loop(){
   digitalWrite(6, LOW);
   delayMicroseconds(2);
   digitalWrite(6, HIGH);
   delayMicroseconds(2);
   digitalWrite(6, LOW);
   
   time = pulseIn(7, HIGH) ;
   
  if(time < 800 && time > 0){
     analogWrite(8, 0);  
   }
}

This code was compiled successfully, but my led doesn't blink at all. But when I used digitalWrite(8, HIGH/LOW) instead of analogWrite it surprisingly worked.

NOTE that, I have used delayMicroseconds. I am guessing that this thing is interfering with the led.

What can I do?

You don't say what Arduino you are trying to use but Pin 8 is not a PWM capable pin on most of them.

Steve

sorry, the codes are just for example only. I am using an Arduino UNO board.

This is the actual code:

#include <Servo.h>
Servo servo;

unsigned long duration = 0;
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t x = 1;

void setup() {
  pinMode(8, INPUT);//  IR sensor
  pinMode(9, INPUT);
  pinMode(12, INPUT);// IR sensor

  pinMode(2, OUTPUT);//IN1
  pinMode(3, OUTPUT);//IN2
  pinMode(4, OUTPUT);//IN3
  pinMode(5, OUTPUT);//IN4
  pinMode(10, OUTPUT);//ENAB
  
  pinMode(6, INPUT);
  pinMode(7, OUTPUT);
  
  //initial state
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  analogWrite(10, 95); //pwm
  
  servo.attach(11);
  servo.write(180);
  delay(1500);

}

void loop() {
   a = digitalRead(8);
   b = digitalRead(9);
   c = digitalRead(12);

  digitalWrite(7, LOW);
  delayMicroseconds(2);
  digitalWrite(7, HIGH);
  delayMicroseconds(2);
  digitalWrite(7, LOW); 
  
  duration = pulseIn(6, HIGH);
   
  if(duration < 800 && duration > 0){
    delay(300); 
    servo.write(95);
    delay(2000); 
  }
    
  if(a == 0 && b == 0 && c == 0){
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW); 
    digitalWrite(2, LOW);  // left  
    delay(60);
  }
  
   else if(a == 0  && c == 0){
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH); // forward
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    delay(5);
  }
  
  else if(a == 0  && c == 1){
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);  // right
    digitalWrite(2, HIGH);
    analogWrite(10, 95);//pwm
    delay(60);
    
  }
  
  else if(a == 1  && c == 0){
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH); //left
    digitalWrite(2, LOW);
    delay(60);
    
  }
   else if(a == 1  && c == 1){
    digitalWrite(4, HIGH);
    digitalWrite(3, HIGH); // forward
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    delay(5);
   }  
}

the servo is working properly. But I cannot get any reply from the pin number 10, though it is fine.

I don't know why you thought it was a good idea to waste everyone's time by posting some code that could never work and that wasn't what you are using but in your latest effort -

Pin 10 is an output pin permanently set to 95. What sort of "reply" do you expect to see and how?

Steve