For a project I'm trying to do, I'd like to be able to convert a variable voltage (between 0v and 12v DC) into a PWM signal (from 0 to 255). Could I have some suggestions on code and components to use to be able to do this with an Arduino please?
You need a voltage divider, for example 10k/7k5, thus you get 0-5V from 0-12V.
Then you read the analog pin (analog pin connected to the middle of the divider), and the value is written to a PWM pin:
..
int voltage = analogRead(pin_analog);
int pwm = map(voltage, 0, 1023, 0, 255);
analogWrite(pin_pwm, pwm);
..