Code assistance requested

Hello, all.
I am seeking assistance for a home cockpit project I’m working on and am at a complete loss as to how to code it.

I want to use a Mega, an L298N H-bridge module (to reverse polarity) for the sake of simplicity, a momentary rocker-type switch with 2 ON poles (center position is OFF) and a relay to drive a 24VDC motor.

The goal is to emulate an aircraft’s electrical trim switch for the elevator which controls pitch movement.

We’ll call the first switch ON position ‘Trim down’ and when activated, the motor spins clockwise. We’ll call the other ON position ‘Trim up’ and when activated, the motor spins counterclockwise. I am thinking that I’ll need two (2) CASE arguments with pin modes reading HIGH or LOW, depending upon which side of the switch is activated and in what direction the motor is to move. This seems easy enough, however, my concern is the inclusion of the relay which is necessary to operate the 24DC motor (no speed adjustments are required…it’s a simple ON/OFF operation). I cannot substitute the nomenclature as the 24VDC motor I have is the only one I’ve found which meets all the required size and torque constraints for this project.

The trim switch is used as needed to trim up or down. There is a sprocket on the motor which will be attached to a bicycle chain to drive two (2) larger wheels via a shaft on both sides of my throttle quadrant.

If you can provide guidance as to how I can achieve this, I will be most grateful. Please accept my apologies in advance if this is not the proper place to post this request.

Thank you very much for your time and help.

I have perused many different resources and references all to no avail. Thus, my posting here.


Yes, that is quite a project. But all projects are made up of many small pieces. So begin with a schematic showing how you have all this wired together and how it is being powered.
From that you can begin coding with the simplest thing and that is a switch. Learn how to determine the position of a switch and you are off to good beginning. Then add a second device and learn how it works and how to code for it.

1 Like


enum { Off = HIGH, On = LOW };

const byte pinMotDir = 11;
const byte pinMotSpd = 10;
const byte pinsLed [] = { pinMotSpd, pinMotDir };

const byte pinButUp   = A1;
const byte pinButDown = A2;
byte pinsBut [] = { pinButUp, pinButDown };

enum { MotFor  = 0, MotRev = 1 };
enum { MotStop = 0, MotSpd = 255 };

// -----------------------------------------------------------------------------
loop ()
    if (LOW == digitalRead (pinButUp))  {
        digitalWrite (pinMotDir, MotFor);
        analogWrite  (pinMotSpd, MotSpd);

    else if (LOW == digitalRead (pinButDown))  {
        digitalWrite (pinMotDir, MotRev);
        analogWrite  (pinMotSpd, MotSpd);

        analogWrite  (pinMotSpd, MotStop);

// -----------------------------------------------------------------------------
setup ()
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);

What do the 'larger wheels' do? Are they just indicators?

What part of this requires the use of a microcontroller?

They’re pretty much for decoration in my case.

All of it…eventually, it will be interfaced with the flight simulator’s operating program.

For the moment, I just need to determine the IF and the HOW.

So it doesn't really matter how far they turn? For decoration, I would use a couple of servos and even mark them to act as trim indicators.

From my brief research, these 'wheels' were originally manual trim wheels that you would turn to adjust trim. Later they were modified with electric motors to allow the trim to be changed with a switch.

Later, on the 737 MAX.....

Yes this happends when systems become too much automated or you hide away how crucial functions work from the pilots trying to be cheaper by pretending the pilots do not need a new training for this airplane type.

Well the answer on the surface is:
is it possible? Yes with almost any microcontroller
HOW can it be done: The answer on the surface is "by programming the needed functionality"

I know very well that this answer does not help at all. You have to be much more specific on what functionality you want to have in the end.

If you don't want to buy a "ready to use" flight-simulator-cockpit off the shelf there is no way around
a) paying a lot of money for somebody doing the coding for you
b) paying quite an amount of time learning to program yourself
best regards Stefan

Re-opened for my mistake.

I cannot thank you enough for pointing-out the obvious to me.

I simply asked for assistance with coding and would appreciate any advice on coding and nothing more.
I am not asking for anyone to do it for me and don’t have the morals to even ask nor suggest that someone do the coding on my behalf.

OK, so what is liked very much in this forum if you post some information about your knowledge-level about programming and about electronics. You can be a real beginner in both this is completely OK. Knowing it will help to adapt answers to your knowledge-level.
And what is liked too is a first attempt how to code it. This attempt does not need to compile without error. If there is a compiler-error this is just the first question: "I get this compiler-error" what must I change to correct it?"

When posting code or compiler-messags use code-tags
for Arduino-code
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

For errormessages
Click into message-console
press Ctrl-A, Ctrl-C
switch to forum
click on the </>-Button
press Ctrl-V
for pasting the messages

code-sections keep the hight of postings small and enable to quickly copy the whole content by a single left-click in the upright-corner

best regards Stefan

Thank you for the replies. Most sincerely appreciated.

I have a moderate knowledge of coding and Arduino and feel comfortable.

Thus far, here is my current code:
N.B. It compiles yet I feel I'm missing something...namely, there are no integers or 'command calls' for the L298N H-bridge and I'm not entirely sure how this would be coded. I know there should be an inclusion for this H-bridge module (to reverse the poles) and this is where I'm stuck--

int switchDNPin = 8;    // switch position 'A'
int switchUPPin = 9;    // switch position 'B'
int pinStatus;

void setup() {
  pinMode(11, OUTPUT);       // to relay
  pinMode(8, INPUT_PULLUP);  // for "Trim UP"
  pinMode(9, INPUT_PULLUP);  // for "Trim DN"


void loop() {
  pinStatus = digitalRead(switchDNPin);
  if (switchDNPin == HIGH) {
    digitalWrite(11, HIGH);
    digitalWrite(8, HIGH);
  pinStatus = digitalRead(switchUPPin);
  if (switchUPPin == HIGH) {
    digitalWrite(11, HIGH);
    digitalWrite(9, HIGH);


Am anxious to learn your collective thoughts.

You'll need two inputs to account for your trim up and trim down and you do. You're writing to them though, which isn't appropriate.

You'll need two outputs for the H-bridge too so you can turn both ways.

And that remains the question.
Do I just attach the motor’s positive and negative power leads to the L298N and include the integers for the H-bridge pins (listed as IN1 and IN2 on the module) as pins 8 and 9 solely? The module also includes an ENA (enable motor ‘A’ from what I’d read) pin…do I need to do anything with that in my coding?
This L298N module can operate two (2) motors at a time.

I haven’t worked with an H-bridge before and appreciate any advice.

8 and 9 are the input pins for your trim switches. You need another two pins set as output to command a single motor on the H-bridge.

Enable you can ignore as a controllable output for now I think, although you may need to tie it high or low to get things working.

If you work with microcontrollers it is in 99% of all cases working with programming and with electronics.

recently I quoogled for something else and google came up with howtomechatronics
this website seems to be beginner-friendly as it explains things easy to understand and takes time to explain the basics.

EDIT: after reading in the comments I revise my recommendation about howtomechatronics:

explains basics easy to understand

but has multiple errors in the code and the schematics.

So the benefit is reduced to can be used to get some basic knowledge

but a different information-source should be used to make your project work.

This is sad but true.

Here ist the link to the section about how to control L298

The picture in the link shows a robot-car. But is has the same basic functionality
drive a DC-motor clockwise CW / counterclockwise CCW using the L298-chip

best regards Stefan