Page 1 of 1

Customizable 0-5V output

Posted: 16 Apr 2020 22:49
by Christian_Bylund
Mario wrote:How can I get a 0-5V output signal compatible with YourDyno?
Warning! Make sure you verify any signals before connecting it to sensitive equipment. Use at your own risk!

Image

The example code already have a PWM function to output an analog signal.

Code: Select all

void UpdateAnalogOutput() {

  //Local constants.
  const float AirFuelRatioOctane = 14.70;
  const int maximumOutput = 51; /* 1V */
  const int minimumOutput = 0;  /* 0V */

  //Local variables.
  int analogOutput = 0;
  float lambdaAFR = Lookup_Lambda(adcValue_UA) * AirFuelRatioOctane;

  //Convert lambda value to PWM output.
  analogOutput = map(lambdaAFR * 100, 2000, 1000, minimumOutput, maximumOutput);

  //Make sure we do not exceed maximum values.
  if (analogOutput > maximumOutput) analogOutput = maximumOutput;
  if (analogOutput < minimumOutput) analogOutput = minimumOutput;
  
  //Set PWM output.
  analogWrite(ANALOG_OUTPUT_PIN, analogOutput);
}
We need to adopt it to output 0V at 7.35 AFR and 5V at 22.39 AFR. We do this by modifying these two lines:
const int maximumOutput = 255; /* 5V */
analogOutput = map(lambdaAFR * 100, 735, 2239, minimumOutput, maximumOutput);


maximumOutput need to reflect the maximum output voltage 0 = 0V; 51 = 1V; 255 = 5V.

The map function is modified with the AFR max and min values * 100. 7.35 AFR at minimumOutput (0V) and 22.39 AFR at maximumOutput (5V). At this point the signal is a PWM signal adjusting the duty cycle with the map function. To filter the signal to a DC-voltage we need to add a RC-filter to the output as the following schematic describes.

Image
To improve response time, reduce resistance from 1k to 470R.

Here is the output signal at 22.39 AFR (5V):
Image

Also check out our video of a 0-1V output for a Lambda Gauge.

Re: Customizable 0-5V output

Posted: 02 Mar 2021 21:20
by huangxiqing
Hello. I don’t write in English, I don’t write in my language, I use a translator.
Can 0-5V linear output and 0-1Vpwm dual output

Re: Customizable 0-5V output

Posted: 10 Mar 2021 11:14
by Christian_Bylund
huangxiqing wrote:
02 Mar 2021 21:20
Hello. I don’t write in English, I don’t write in my language, I use a translator.
Can 0-5V linear output and 0-1Vpwm dual output
Yes. That is the great thing about Arduino, as long as you have more pins available you can configure them to output whatever you need.