# DC Motor Control with a single button and H Bridge

**URL:** <https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556>\
**Category:** General Guidance\
**Created:** [February 21, 2025, 7:18pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556 "2025-02-21T19:18:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![goose\_1709](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goose_1709/32/1198867_2.png) [@goose\_1709](https://forum.arduino.cc/u/goose_1709)\
**Post date:** [February 21, 2025, 7:18pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/1 "2025-02-21T19:18:36Z")

</div>

Hi all, I'm new to Arduino with no coding experience. I'm slowly working through a few tutorials to learn but I am still in the beginning phases, learning very basic code. I'm using an Arduino Uno board and for the past month, I have been fighting with a piece of code that I found online, that I modified to stop, start and change the direction of a dc motor with a single button. One press should start the motor, the second press should stop the motor. when the button is pressed again, it should start the motor in the opposite direction and another press should stop it again. The code which I will paste below, starts on the first press and stops on the second press as it should, the next press starts it again but sometimes in the opposite direction as it should but sometimes in the same direction as before which tells me that I have some error where it should change direction with every second press. Can you guys have a look at it and tell me where I'm going wrong. I would like to include a schematic but the system does not allow newbies to add attachments.

```cpp
int IN1pin = 7;
int IN2pin = 8;
int ENApin = 9;
int motorSpeed;
int buttonPin = 3;
int motorState;
int lastButtonState;
int currentButtonState;

void setup() {
  // put your setup code here, to run once:
  pinMode(IN1pin, OUTPUT);
  pinMode(IN2pin, OUTPUT);
  pinMode(ENApin, OUTPUT);
  pinMode(buttonPin, INPUT);

  currentButtonState = digitalRead(buttonPin);
}

void loop() {
  // put your main code here, to run repeatedly:
  lastButtonState = currentButtonState;
  currentButtonState = digitalRead(buttonPin);

  if (lastButtonState == HIGH && currentButtonState == LOW) {

    //toggle state of motor
    if (motorState == LOW) {
      motorState = HIGH;
    } else {
      motorState = LOW;
    }

    //control motor according to toggled state
    digitalWrite(IN1pin, motorState); //turns motor on or off, based on variable
    digitalWrite(IN2pin, LOW);
    analogWrite(ENApin, 255);
  }

  lastButtonState = currentButtonState;
  currentButtonState = digitalRead(buttonPin);

  if (lastButtonState == HIGH && currentButtonState == LOW) {

    //toggle state of motor
    if (motorState == LOW) {
      motorState = HIGH;
    } else {
      motorState = LOW;
    }

    //control motor according to toggled state
    digitalWrite(IN2pin, motorState); //turns motor on or off, based on variable
    digitalWrite(IN1pin, LOW);
    analogWrite(ENApin, 255);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 21, 2025, 7:24pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/2 "2025-02-21T19:24:41Z")

</div>

> [@goose\_1709](#):
>
> One press should start the motor, the second press should stop the motor. when the button is pressed again, it should start the motor in the opposite direction and another press should stop it again.

That seems to be FOUR possible conditions, but you don't have that many in your code.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [February 21, 2025, 7:33pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/3 "2025-02-21T19:33:12Z")

</div>

Please edit your post to add code tags.

As pointed out, there are four operational states. It would be a good idea to implement a 4 state "state machine" (look that up).

The press of the button would simply advance machine states cyclically 1-\>2-\>3-\>4-\>1 etc.

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [February 21, 2025, 8:36pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/4 "2025-02-21T20:36:20Z")

</div>

I recommend you get a copy of the Arduino Cookbook and skim it cover to cover and try any of the projects that interest you.

---

<div class="post-metadata">

**Author:** ![HazardsMind](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hazardsmind/32/215209_2.png) [@HazardsMind](https://forum.arduino.cc/u/HazardsMind)\
**Post date:** [February 21, 2025, 10:07pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/5 "2025-02-21T22:07:15Z")

</div>

You want to make what is known as a “State Machine”

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [February 22, 2025, 7:30am UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/6 "2025-02-22T07:30:07Z")

</div>

Hi, @goose_1709  
Welcome to the forum.

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behavior and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frus…

This will show you how to post your code in a scrolling window.

Tom.... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![goose\_1709](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goose_1709/32/1198867_2.png) [@goose\_1709](https://forum.arduino.cc/u/goose_1709)\
**Post date:** [February 23, 2025, 4:11pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/7 "2025-02-23T16:11:21Z")

</div>

Thank you, I am now looking at "State Machine" and how it works.

---

<div class="post-metadata">

**Author:** ![goose\_1709](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goose_1709/32/1198867_2.png) [@goose\_1709](https://forum.arduino.cc/u/goose_1709)\
**Post date:** [February 23, 2025, 4:14pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/8 "2025-02-23T16:14:19Z")

</div>

Thank You  
I have edited my post from top to bottom, including the question that I asked, I will do it right from now.

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 23, 2025, 4:17pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/9 "2025-02-23T16:17:26Z")

</div>

> [@goose\_1709](#):
>
> I have edited my post from top to bottom,

UNless you also edited all the answers to your questions, the thread no makes no sense to anyone reading the thread. Why did you do that?

---

<div class="post-metadata">

**Author:** ![goose\_1709](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goose_1709/32/1198867_2.png) [@goose\_1709](https://forum.arduino.cc/u/goose_1709)\
**Post date:** [February 23, 2025, 4:23pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/10 "2025-02-23T16:23:41Z")

</div>

Thank you for the reply, I have edited my post, hope it looks better now, also busy reading all about how "State Machine" works. Perhaps once I understand it, I could solve my problem by starting with complete new code including the four state machine.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [February 23, 2025, 4:26pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/11 "2025-02-23T16:26:27Z")

</div>

> [@goose\_1709](#):
>
> I have edited my post

Please don't, other than add code tags. Editing content makes the thread difficult or even impossible for others to follow.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 22, 2025, 4:26pm UTC](https://forum.arduino.cc/t/dc-motor-control-with-a-single-button-and-h-bridge/1356556/12 "2025-08-22T16:26:55Z")

</div>

Closed on August 22, 2025, 4:26pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
