buddhimaan
Published © GPL3+

Arduino IoT Train

An IoT controlled LEGO Tram that can move forward and backwards, and also has individually controllable LED channels.

IntermediateFull instructions provided6,924
Arduino IoT Train

Things used in this project

Hardware components

Arduino Nano 33 IoT
Arduino Nano 33 IoT
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
DC Motor, Miniature
DC Motor, Miniature
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Resistor 1k ohm
Resistor 1k ohm
×1
LED (generic)
LED (generic)
×4
Jumper wires (generic)
Jumper wires (generic)
×15
9V battery (generic)
9V battery (generic)
×2
9V Battery Clip
9V Battery Clip
×2

Software apps and online services

Arduino IoT Cloud
Arduino IoT Cloud
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

Schematic

Circuit Diagram using Altium CircuitMaker (since I don't have fritzing)

Code

Code

Arduino
This is the Code that needs to be uploaded to the Nano 33 IoT for the train to function. You can modify it if you want to add anything extra, but you will need to change the controlled things inside the Arduino IoT Cloud
/*
  Controlled Variables through Arduino IoT Cloud

  bool speedReset;
  bool direction;
  int speed;
  int speedControl;
  CloudDimmedLight led;
*/

#include "thingProperties.h"

//Set LED Channels and variables for control
int ledC1 = 2;
int ledC2 = 3;
int ledC3 = 4;
int ledC4 = 5;
int DUTY = 0;
bool ledSwitch = true;

//Set Speed Control Pins
int SC_A = 12;
int SC_B = 8;

//Set Motor Pins
int MF_A = 10;
int MB_A = 11;

int MF_B = 6;
int MB_B = 7;

//Set values for speed control
int MDUTY = -255;
int MDUTY_N = 0;
int Motor_Speed;

void setup() {
  // Initialize serial monitor:
  Serial.begin(9600);
  Serial.println("Serial Monitor Initialized");
  
  pinMode(ledC1, OUTPUT);
  pinMode(ledC2, OUTPUT);
  pinMode(ledC3, OUTPUT);
  pinMode(ledC4, OUTPUT);
  
  pinMode(SC_A, OUTPUT);
  pinMode(SC_B, OUTPUT);
  pinMode(MF_A, OUTPUT);
  pinMode(MF_B, OUTPUT);
  pinMode(MB_A, OUTPUT);
  pinMode(MB_B, OUTPUT);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  if(!ArduinoCloud.begin(ArduinoIoTPreferredConnection))
  {
    Serial.println("Failed to Connect to wifi or UBLOX not initialized");
  }
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the ledDUTYer number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

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



void onSpeedControlChange() 
{
  Motor_Speed = speedControl;
  MDUTY = Motor_Speed;
  
  if(MDUTY > 0)
  {
    speed = MDUTY;
    direction = true;
    
    //Set the speed of the motors
    analogWrite(SC_A, MDUTY);
    analogWrite(SC_B, MDUTY);
    
    //Power Motors to turn forward
    digitalWrite(MF_A, HIGH);
    digitalWrite(MF_B, HIGH);
    
    digitalWrite(MB_A, LOW);
    digitalWrite(MB_B, LOW);
  }
  
  if(MDUTY < 0)
  {
    //Set MDUTY_N as the absolute value of MDUTY for speed
    MDUTY_N = abs(MDUTY);
    speed = MDUTY_N;
    direction = false;
    
    //Set the speed of the motors
    analogWrite(SC_A, MDUTY_N);
    analogWrite(SC_B, MDUTY_N);
    
    //Power Motors to turn backward
    digitalWrite(MF_A, LOW);
    digitalWrite(MF_B, LOW);
    
    digitalWrite(MB_A, HIGH);
    digitalWrite(MB_B, HIGH);
  }
  
  if(MDUTY == 0)
  {
    speed = MDUTY;
    //Set the speed of the motors to 0
    analogWrite(SC_A, MDUTY);
    analogWrite(SC_B, MDUTY);
    
    //Turn off Motors
    digitalWrite(MF_A, LOW);
    digitalWrite(MF_B, LOW);
    
    digitalWrite(MB_A, LOW);
    digitalWrite(MB_B, LOW);
  }
}



void onSpeedResetChange() 
{
  if(speedReset == true)
  {
    speedControl = 0;
    speed = 0;
    //Set the speed of the motors to 0
    analogWrite(SC_A, MDUTY);
    analogWrite(SC_B, MDUTY);
    
    //Turn off Motors
    digitalWrite(MF_A, LOW);
    digitalWrite(MF_B, LOW);
    
    digitalWrite(MB_A, LOW);
    digitalWrite(MB_B, LOW);
  }
  
}


void onLedChange() 
{
  if(led.getSwitch() == ledSwitch)
  {
    
    DUTY = led.getBrightness() * 2.5;
    Serial.println(DUTY);
    
    analogWrite(ledC1, DUTY);
    analogWrite(ledC2, DUTY);
    analogWrite(ledC3, DUTY);
    analogWrite(ledC4, DUTY);
    
    digitalWrite(ledC1, HIGH);
    digitalWrite(ledC2, HIGH);
    digitalWrite(ledC3, HIGH);
    digitalWrite(ledC4, HIGH);
  }
  
  else
  {
    DUTY = 0;
    analogWrite(ledC1, DUTY);
    analogWrite(ledC2, DUTY);
    analogWrite(ledC3, DUTY);
    analogWrite(ledC4, DUTY);
    
    digitalWrite(ledC1, LOW);
    digitalWrite(ledC2, LOW);
    digitalWrite(ledC3, LOW);
    digitalWrite(ledC4, LOW);
  }
}

Credits

buddhimaan

buddhimaan

3 projects • 5 followers

Comments