0-10V to PWM conversion software issue

I'm trying to use an Arduino to be able to convert 0-10V analog voltage into 5V PWM, using the arduino. (I know that it can be done with a simple opamp, but that's not the route I need to go with this project)

On the input, I have a voltage divider, as well as a RC high-pass filter. The output signal for some reason won't go to "0%" PWM, it just hangs at a fraction of a percentage. I've even tried multiple pull-down resistor values on the outputs, and that doesn't change the signal at all on the DSO.

That said, it sounds like the Arduino is outputting an active signal when it should be completely off, which explains why the signal cannot be pulled down to 0%.

Here is the code I'm using, please feel free to tell me what I am doing wrong!

 void loop() {
 newVal = analogRead(0);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(3, newVal/4);
    }
  newVal = analogRead(1);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(5, newVal/4);
    }
    newVal = analogRead(2);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(6, newVal/4);
    }
    newVal = analogRead(3);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(10, newVal/4);
    }
    newVal = analogRead(4);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(9, newVal/4);
    }
    newVal = analogRead(5);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(11, newVal/4);
 }
 }

The output signal for some reason won't go to "0%" PWM, it just hangs at a fraction of a percentage.

newVal = analogRead(0);
 if (abs(newVal - val) > 4){
   val = newVal;
   analogWrite(3, newVal/4);
    }

The minimum value that you pass to analogWrite is 1. Why do you expect it to go to zero?

Cattledog, Thanks for the reply. Do you have a solution to get the analogWrite to go to 0?

Thanks.

analogWrite(pin,0)

Take a look at the source code for analogWrite() in hardware/arduino/cores/arduino/wiring_analog.c

When the function gets a value of 0 or 255 it actually writes the pin LOW or HIGH. Any other values start using timer settings and will have some output.

What about if I go this route, see any issues with it? If so, please post some type of solution, I'm standing by ready to test your solutions out.

Thanks

void loop()  {
 int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);  

val = analogRead(1);
val = map(val, 0, 1023, 0, 255);
analogWrite(10, val);

val = analogRead(2);
val = map(val, 0, 1023, 0, 255);
analogWrite(11, val);

val = analogRead(3);
val = map(val, 0, 1023, 0, 255);
analogWrite(3, val);

val = analogRead(4);
val = map(val, 0, 1023, 0, 255);
analogWrite(5, val);

val = analogRead(5);
val = map(val, 0, 1023, 0, 255);
analogWrite(6, val);

}

You can simplify those to:

void loop()  {
    analogWrite(9, map(analogRead(0), 0, 1023, 0, 255));  
    analogWrite(10, map(analogRead(1), 0, 1023, 0, 255));  
    analogWrite(11, map(analogRead(2), 0, 1023, 0, 255));  
    analogWrite(3, map(analogRead(3), 0, 1023, 0, 255));  
    analogWrite(5, map(analogRead(4), 0, 1023, 0, 255));  
    analogWrite(6, map(analogRead(5), 0, 1023, 0, 255)); 
}

And that is equivalent to:

void loop()  {
    analogWrite( 9, analogRead(0) / 4);  
    analogWrite(10, analogRead(1) / 4);  
    analogWrite(11, analogRead(2) / 4);  
    analogWrite( 3, analogRead(3) / 4);  
    analogWrite( 5, analogRead(4) / 4);  
    analogWrite( 6, analogRead(5) / 4); 
}

Bump. I still haven't received a solution that outputs 0% PWM. Can someone please help?

Thanks!

johnwassers revision of your mapping solutions (and indeed your mapping code) should give you 0 for analog read values <4. Did you try either one?

Ok, I first tried it, but forgot to remap the pins. After remapping the pins, it now works.

I sincerely thank you all for your assistance with this, I greatly appreciate it!