Arduino MKR WiFi 1010 Buttom +DC motor

Hello, I'm trying to turn ON/OFF a DC motor from Arduino Iot Cloud. I have to turn it ON/OFF physically and through the cloud, physically it works well but from the cloud it does nothing. I'm using an Arduino MKR WiFi 1010, a L298N Dual full bridge,9V DC motor and a push buttom.

I tried with a LED and it wprked very well, but with the motor doesn't. My code is

#include "thingProperties.h"
#include <FTDebouncer.h>

#define MOTOR_PIN 2
#define BUTTON_PIN 5

FTDebouncer buttons;

void setup() {
  pinMode(MOTOR_PIN, OUTPUT);
  buttons.addPin(BUTTON_PIN, LOW);
  buttons.init();

  setDebugMessageLevel(2);
  Serial.begin(9600);
  delay(1500);
  // Defined in thingProperties.h
  initProperties();
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}

void loop() {
  buttons.update();
  ArduinoCloud.update();
  onMotorChange();
}

void onPinActivated(int pinNr) {
  // do something according to the _pinNR that is triggered. For instance:
  Serial.println(pinNr);
  toggle = !toggle;
}

void onPinDeactivated(int pinNr) {
  // do something according to the _pinNR that is triggered. For instance:
  Serial.println(pinNr);
}
void onMotorChange() {
  digitalWrite(MOTOR_PIN, toggle || motor);
  Serial.print("The motor is ");
  if (toggle) {
    Serial.println("ON");
  } 
}

Thanks for help

Is there a possibility of adding a relay to control the on and off via the cloud?

Sorry, How should I do that? How should I connect it or declare it?

Do you have a relay to connect to your setup?

Its pretty simple to wire up and use, if you are just looking to turn on and off the motor, but if you are looking to control the direction of the motor, that is a few more steps, but still doable.

Here is a sample on how to use your bridge with a relay.

And if you are looking to control the direction of the motor, take a look at this link

Thank you, I'll test it.

Let us know if you have any issues or unable to make it work, looks simple, but not always the case.

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