0

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. Mi circuit is like this enter image description here

My code is this

#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");
  } 
}

I tried with a LED and it worked very well, but with the motor doesn't. This is the code that Arduino creates when I add things to the board, the part of

thingProperties.h

// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

// x-ed id
const char THING_ID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_PASS;    // Network password (use for WPA, or 
use as 
key for WEP)

void onMotorChange();

bool toggle;
bool motor;

void initProperties(){

ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(toggle, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(motor, READWRITE, ON_CHANGE, onMotorChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Thanks for the help and suggestions. How should I connect the motor or what do I have to change in the code?

6
  • You have connected the button direct to the driver board: When you press the button, the Arduino is not involved. It recognizes the button press but its reaction doesnt matter. You can not say your button works with the Arduino program. You defined the motor output signal to pin 2 but you used pin 3 to connect it to the motor driver. You used different inputs on the driver (for the arduino signat and the button signal). It is possible that the other input is for a second motor. And finnally your program has logic that make no sense; I doubt it compile at all. e.g. where is toggle declared? Mar 3, 2021 at 8:22
  • @PeterPaulKiefer it might be declared in thingProperties.h, but that's unclear. Mar 3, 2021 at 8:40
  • @PythonSchlange Yes that's possible. The OP is welcome to clearify that. Although toggle is a very generic name for a variable that is declared in a header file. And e.g. ArduinoCloud.update() could play a serious role. But I have no access to it. As far as I know the program, it makes no sense to me. Mar 3, 2021 at 8:49
  • @PeterPaulKiefer I totally agree with you. Please upload the header file too. Mar 3, 2021 at 10:29
  • @PeterPaulKiefer I was wrong making the diagram, just that. Toggle was declared in 'thingProperties.h', when you add your thing, the cloud generates a code Mar 3, 2021 at 20:17

1 Answer 1

1

The following code is an example. As I don't know the Arduino Cloud Thing configuration, and I don't have access to the cloud and the Hardware, the programm is just a guess making some commented assumptions.

There are many assuptions I made, and I can not rely on the schematics you showed me. Eventually you have to redesign the circuit/hardware setup. A L298N is a so called H-Bridge. That needs at least two control pins in1 and in2 connected to arduino outputs to define the direction of the motor. You only connected one of them to an output of the arduino. And one of them to the button and an input of the arduino. (If that part of the schematics is correct.)

If you press the button in1 gets HIGH but the in2 may also get HIGH because your program switches the motor pin to HIGH. That turns the motor off. If one pin is HIGH the other must be LOW to drive the motor. Some L298N modules also need a PWM signal at the enA pin. That sets the speed of the motor with the duty cycle of the PWA signal. Most modules have a pullup at enA and enB so per default the motors are configured for full speed.

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

#define MOTOR_PIN 2
#define BUTTON_PIN 5

FTDebouncer buttons;

void setup() 
{
  pinMode(MOTOR_PIN, OUTPUT);

  // !!! PPK: Handler methods added
  buttons.addPin(BUTTON_PIN, LOW, onPinActivated, onPinDeactivated);

  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();
}

void onPinActivated(int pinNr) 
{
  if ( pinNr == BUTTON_PIN )
  {
     toggle = !toggle;
     // this assumes that the toggle triggers a change for
     // the motor variable in arduino cloud. If not, 
     // the motor variable will not be changed and my code does not work.
     // uncommenting the following line may help, in that case
     // motor = toggle;
  }
}

void onPinDeactivated(int pinNr) 
{
  // Intentionally left empty, we only react to the rising edge.
}

void onMotorChange() 
{
  digitalWrite( MOTOR_PIN, motor );
  Serial.print("The motor is ");
  if ( motor ) 
  {
    Serial.println("ON");
  } 
  else
  {
    Serial.println("OFF");
  }
}
1
  • This line // !!! PPK: Handler methods added buttons.addPin(BUTTON_PIN, LOW, onPinActivated, onPinDeactivated); (?) I can't compile with this and I can't edit "thingProperties.h". BUTTON_PIN is the pin for toggle that is a bool thing as the motor, just to turn ON/OFF (I follow an example in Arduino project hub) Mar 5, 2021 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.