# Changing the output voltage of a PWM signal

I am using an Arduino mega 2560 and I want to use Analogwrite to output a PWM voltage to an optical sensor but the sensor requires a pulsing signal from 3.5V to 5V. Since the voltage of the PWM alternates from 0V to 5V, I want to possibly output 0 to 1.5V out of the PWM then add 3.5V with a voltage divider to the signal to then put it into the sensor. I want to know if it is possible to change the PWM output voltage to 1.5V or if I should just use two voltage dividers.

Could you post the make/model of the optical sensor?

If you feed 3.5 volts via onboard 5v and resistor to the PWM output pin going to the sensor you will bounce from 3.5 to 5v.

Are you sure you read the data sheet correctly? It might have meant you feed it a pulse train with amplitude from 0-3.5V or 0-5V.

Can you tell us the name and specs of this sensor?

It is a Microphotonic Devices (Micro-displacement Sensor) Z4D-C01.
The spec sheet is below.

I may be missing it, that's a pretty "cluttered" data sheet, but I can't see anything about the pulse voltage.

Can you quote what it says, or do a screen capture and highlight it?

edit: I'm going to guess though and agree with Power_Broker, that it means the high voltage needs to be 3.5 to 5, with a low of 0.

another edit: The flyer says this:

I reckon Power_Broker's right.

That's what I thought, but I couldn't get it to work with the basic Arduino code with PWM output and the duty cycle the sensor spec sheet recommends. The code ends up just printing the same 4.68 volts constantly. Maybe my code is wrong. I've written it out below.

int sensorpin = 0; // analog pin used to connect the sensor (Green wire)
int PWMpin = 2; // digital PWM pin to output to sensor (Red wire)
int val = 0; // variable to store the values from sensor(initially zero)
int duty_cycle = 25; // Outputs 0-255 where 0 is 0% and 255 is 100%
//float res = 0.01; // Resolution in mm (10 micrometers)
double dist_val = 0;
double volt_val = 0;
void setup() // Black wire is ground and Yellow wire is power (5v)
{
Serial.begin(9600); // starts the serial monitor
pinMode(sensorpin, INPUT); // declare sensorpin as an input
pinMode(PWMpin, OUTPUT); // declare PWMpin as an output
}

void loop()
{
analogWrite(PWMpin, duty_cycle); // generates a modulated signal output from PWMpin
volt_val = (val/1023.0)5; // converts signal out put to volts
//dist_val = val
0.0714285714; // converts voltage output to mm
Serial.println(volt_val); // prints the value of the sensor to the serial monitor
delay(10); // wait for this much time before printing next value
}

Here's a couple of notes:

2.) If the duty cycle never changes, only call analogWrite() once in setup() and don't touch it in loop().
3.) You're better off not even using analogWrite() AT ALL considering the example timing diagram below. It looks like when you feed the sensor a single pulse, the sensor takes some time to initialize and then sends out readings once every 80 microseconds. TBH, you could probably just do a digitalWrite(pin, HIGH) in setup and then do readings whenever you want to.

I think the way you have the code now (with a PWM signal) is that you happen to be polling the output during initialization and not when the sensor is outputting a distance - just a shot in the dark, though.

So in measuring the pulse wave out of the PWM pin, the signal is correct in providing 0V-5V waveform. However, once it is plugged in to the pls pin on the sensor, the waveform changes to 3.8V-5V. This seems like it may be the issue?

sambrappaport:
So in measuring the pulse wave out of the PWM pin, the signal is correct in providing 0V-5V waveform. However, once it is plugged in to the pls pin on the sensor, the waveform changes to 3.8V-5V. This seems like it may be the issue?

I’m having difficulty understanding what you meant by that but here’s a few notes:

1.) You’re not supposed to be measuring the output of any PWM pin
2.) You should be outputting a constant 5V signal to the pulse input pin (sounds odd, but it’s what you’re supposed to do unless you want to put the module into sleep mode)
3.) There shouldn’t be any 3.8-5V waveforms

sambrappaport:
I am using an Arduino mega 2560 and I want to use Analogwrite to output a PWM voltage to an optical sensor but the sensor requires a pulsing signal from 3.5V to 5V. Since the voltage of the PWM alternates from 0V to 5V, I want to possibly output 0 to 1.5V out of the PWM then add 3.5V with a voltage divider to the signal to then put it into the sensor. I want to know if it is possible to change the PWM output voltage to 1.5V or if I should just use two voltage dividers.

I do not think you require a voltage divider from looking at the spec sheet. The PLS is rated for up to 7V. The connection to the uProcessor is a digital out. The Block diagram shows the trigger xistor is connected to ground. The signal voltage spec reads 3.5 volts to 5 volts. Does not mean switch from 3.5 volts to 5 volts. Means the trigger voltage can be in the range of 3.5V to 5 volts. The output voltage reads that the output is from .2volts to VCC.

Look carefully at the output of the uProcessor. It reads D port ( Digital port ). Digital is not driven from 3.5V to 5V, it is driven from 0V to VDDoperating.

I finally got it to work. It turned out it did in fact need a pulsing signal from 3.5V to 5V even though it is unclear in the spec sheet. I believe it was reading a constant value before because each time the voltage got to 0 it would reset the sensor and cause the initiation setup over and over. I used a voltage divider to get the arduino's pulse output from 0-5V to 0-1.5V then put a power supply of 3.5 V in series to get 3.5-5V.