 # 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() {
if (abs(newVal - val) > 4){
val = newVal;
analogWrite(3, newVal/4);
}
if (abs(newVal - val) > 4){
val = newVal;
analogWrite(5, newVal/4);
}
if (abs(newVal - val) > 4){
val = newVal;
analogWrite(6, newVal/4);
}
if (abs(newVal - val) > 4){
val = newVal;
analogWrite(10, newVal/4);
}
if (abs(newVal - val) > 4){
val = newVal;
analogWrite(9, newVal/4);
}
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()  {
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);

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

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

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

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

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()  {