Using PWM output as voltage?

EAL727Capt

Well-known member
Hello, all and happy holidays!

Am working on my Boeing 727-200 home cockpit using (primarily) Teensy 4.1 boards (as they are all that seem to be available anywhere these days).

I'm currently playing with the interfacing of a Horizontal Situation Indicator and am attempting to get two (2) needles to move in concert with what's happening in X-Plane 11.55 and am not having any success.

On my instrument, I'm focusing my attention on four (4) different pins:
Pins 32 and 31 control the 'Course deviation' bar
Pins 38 and 37 control the 'Glide Slope' bar

As a bench test, when I apply 2V (from a 28V power supply) I get movement--
Pin 32+ / Pin 31- gives full deflection to the right at 2V
Pin 31+ / Pin 32- gives full deflection to the left at 2V

Pin 38+ / Pin 37- gives full deflection UP at 2V
Pin 37+ / Pin 38- gives full deflection DOWN at 2V

The sketch I'm using (and I'm not sure where I got it from......it might be Rob Archer) is below.

Can someone please guide me as to what is not right here or what I'm not doing correctly?

Help is appreciated, as always.

Jay




Code:
// Code for Course Dev Bar and GS on HSI

FlightSimFloat crsBar;  //-2.0 to 2.0  2 dot deflection
FlightSimFloat gsBar;   //-2.0 to 2.0  2 dot deflection
#define CourseDeviation 10
#define GsDeviation 9

//setup runs once
void setup(){
  setupCourseDeviation();
  setupGsDeviation();
}

//loop runs repetitively, as long as power is on
void loop(){
  FlightSim.update(); // causes X-Plane's changes to be received
  updateCourseDeviation();
  updateGsDeviation();
}

voidsetupCourseDeviation(){
  crsBar = XPlaneRef("sim/cockpit/radios/nav1_hdef_dot");
  pinMode(CourseDeviation, OUTPUT) // PWM out
}

void updateCourseDeviation(){
  // crsBar -2.0 to 2.0  2 dot deflection
  int dev = (crsBar * 127); // dev = Deviation of course bar
  dev = map(dev, -254, 254, 0, 255);
  analogWrite(CourseDeviation, dev);
  }

void setupGsDeviation(){
  gsBar = XPlaneRef("sim/cockpit/radios/nav1_vdef_dot");
  pinMode(GsDeviation, OUTPUT);  // PWM out
}

void updateGsDeviation(){
  //crsBar -2.0 to 2.0  2 dot deflection
  int gsdev = (gsBar * 127); // dev = Deviation of GS bar
  gsdev = map(gsdev, -254, 254, 0, 255);
  analogWrite(GsDeviation, gsdev);
}
 
I just tried the following code on a 4.1 and got expected PWM results.

Code:
void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  analogWrite(9, 50);
}

add the line Serial.println(gsdev); before analogWrite(GsD...)

what do you get?
 
Thank you so very much for the reply and your suggestion.

I’m away from my setup right now, running the traditional before Christmas errands…and will amend when I get back home.

Out of curiosity, are you able to determine the voltage at pin 9 when PWM is active? My unit’s requirement is anywhere from 0 to 2 volts.
Also….what is the ‘50’ in the ‘analogWrite(9, 50);’ line? If I had to guess, I’d say it’s duty time (50%) but never having worked with PWM previously, it’s just a guess.

Thank you again!!!
 
50 was may hard coded byte value so the duty cycle = 50/255.

With your mapping function, the PWM will will a square wave either 0 or3v3, but an average read will provide 0 TO 3v3. You will need to mess with values in your mapping function to output 0 to 2 volts with the input range. I'll guess this:

dev = map(dev, -254, 254, 0, 155); // initial guess: 3.3/255 = 2.0/x


I'm not sure if your gages can handle a PWM signal. If they can't handle a PWM you can put a large cap (say 100 uf) across pin 9 and ground to smooth the square wave to a smoother constant voltage.
 
Kris;
Thank you very much for your replies and suggestions and please forgive my delay in replying. I was called to fly to the east coast due to a death in my family and got stuck in all the bad weather while trying to get back home to Las Vegas. It took me 4 days to make it back after no less than 11 cancelled flights and re-routes. I made it home on Christmas Day having to drive in from L A. To say that the travel situation was a complete meltdown would be a severe understatement.

But…I’m home and this week, will take a hard look at am ending my sketch based on your recommendations. I will report here on my findings.

I did receive what may be the last Teensy LC in the world and am wondering if this board would be a better option.

As a reminder, my instrument requires both positive and negative power to move the needle to full deflection up and down and I am convinced that there is a way to accomplish this via Teensy but haven’t discovered how to accomplish this yet.

Stay tuned and again, my profound gratitude.

Jay
 
(Initial comment deleted . . . I should have gone back to read the full thread before posting my initial comment).

OK, so let me try this again . . .

To drive a meter/coil/needle both positive & negative, you could possibly use one of two approaches:

APPROACH #1
=========

Tie the negative side of the meter coil to the common point - call it REF - of a resistor divider that splits the 3.3VDC output in half (10K #1 & 10K #2 tied together at one end (REF) , the other end of 10K #1 tied to 3.3VDC & the other end of 10K #2 tied to GND). The junction of the two resistors (REF) would then be at 3.3VDC / 2, or 1.65VDC. Tie the positive side of the meter coil to a Teensy pin (possibly with an inline resistor if/as needed to accommodate the full-scale voltage of the meter coil). When the Teensy pin is driven with a PWM level, where the resulting PWM voltage is greater than 1.65VDC, the needle would deflect proportionally in the positive direction. Likewise, when the Teensy pin is driven with a PWM level, where the resulting PWM voltage is less than 1.65VDC, the needle would deflect proportionally in the negative direction. This approach could work as long as the full-scale voltage of the meter coil is 1.65VDC or less, & uses one Teensy pin & two resistors (minimum) per meter/needle.

APPROACH #2
=========

Tie the negative side of the meter coil to Teensy Pin A (possibly with an inline resistor if/as needed to accommodate the full-scale voltage of the meter coil). Tie the positive side of the meter coil to Teensy pin B (possibly with an inline resistor if/as needed to accommodate the full-scale voltage of the meter coil). When Teensy pin A is at GND & Teensy pin B is driven with a PWM level, the needle would be deflected proportionally in the positive direction. When Teensy pin B is at GND & Teensy pin A is driven with a PWM level, the needle would be deflected proportionally in the negative direction. This approach could work as long as the full-scale voltage of the meter coil is 3.3VDC or less, & uses two Teensy pins per meter/needle.

Hope that helps . . .

Mark J Culross
KD5RXT
 
Last edited:
Back
Top