Arduino IoT Train
An IoT controlled LEGO Tram that can move forward and backwards, and also has individually controllable LED channels.
Components and supplies
9V Battery Clip
Resistor 1k ohm
Jumper wires (generic)
Arduino Nano 33 IoT
DC Motor, Miniature
Solderless Breadboard Half Size
Dual H-Bridge motor drivers L293D
9V battery (generic)
LED (generic)
Apps and platforms
Arduino IoT Cloud
Arduino Web Editor
Project description
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
1/* 2 Controlled Variables through Arduino IoT Cloud 3 4 bool speedReset; 5 bool direction; 6 int speed; 7 int speedControl; 8 CloudDimmedLight led; 9*/ 10 11#include "thingProperties.h" 12 13//Set LED Channels and variables for control 14int ledC1 = 2; 15int ledC2 = 3; 16int ledC3 = 4; 17int ledC4 = 5; 18int DUTY = 0; 19bool ledSwitch = true; 20 21//Set Speed Control Pins 22int SC_A = 12; 23int SC_B = 8; 24 25//Set Motor Pins 26int MF_A = 10; 27int MB_A = 11; 28 29int MF_B = 6; 30int MB_B = 7; 31 32//Set values for speed control 33int MDUTY = -255; 34int MDUTY_N = 0; 35int Motor_Speed; 36 37void setup() { 38 // Initialize serial monitor: 39 Serial.begin(9600); 40 Serial.println("Serial Monitor Initialized"); 41 42 pinMode(ledC1, OUTPUT); 43 pinMode(ledC2, OUTPUT); 44 pinMode(ledC3, OUTPUT); 45 pinMode(ledC4, OUTPUT); 46 47 pinMode(SC_A, OUTPUT); 48 pinMode(SC_B, OUTPUT); 49 pinMode(MF_A, OUTPUT); 50 pinMode(MF_B, OUTPUT); 51 pinMode(MB_A, OUTPUT); 52 pinMode(MB_B, OUTPUT); 53 54 // Defined in thingProperties.h 55 initProperties(); 56 57 // Connect to Arduino IoT Cloud 58 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 59 60 if(!ArduinoCloud.begin(ArduinoIoTPreferredConnection)) 61 { 62 Serial.println("Failed to Connect to wifi or UBLOX not initialized"); 63 } 64 65 /* 66 The following function allows you to obtain more information 67 related to the state of network and IoT Cloud connection and errors 68 the ledDUTYer number the more granular information you’ll get. 69 The default is 0 (only errors). 70 Maximum is 4 71 */ 72 setDebugMessageLevel(2); 73 ArduinoCloud.printDebugInfo(); 74} 75 76void loop() 77{ 78 ArduinoCloud.update(); 79} 80 81 82 83void onSpeedControlChange() 84{ 85 Motor_Speed = speedControl; 86 MDUTY = Motor_Speed; 87 88 if(MDUTY > 0) 89 { 90 speed = MDUTY; 91 direction = true; 92 93 //Set the speed of the motors 94 analogWrite(SC_A, MDUTY); 95 analogWrite(SC_B, MDUTY); 96 97 //Power Motors to turn forward 98 digitalWrite(MF_A, HIGH); 99 digitalWrite(MF_B, HIGH); 100 101 digitalWrite(MB_A, LOW); 102 digitalWrite(MB_B, LOW); 103 } 104 105 if(MDUTY < 0) 106 { 107 //Set MDUTY_N as the absolute value of MDUTY for speed 108 MDUTY_N = abs(MDUTY); 109 speed = MDUTY_N; 110 direction = false; 111 112 //Set the speed of the motors 113 analogWrite(SC_A, MDUTY_N); 114 analogWrite(SC_B, MDUTY_N); 115 116 //Power Motors to turn backward 117 digitalWrite(MF_A, LOW); 118 digitalWrite(MF_B, LOW); 119 120 digitalWrite(MB_A, HIGH); 121 digitalWrite(MB_B, HIGH); 122 } 123 124 if(MDUTY == 0) 125 { 126 speed = MDUTY; 127 //Set the speed of the motors to 0 128 analogWrite(SC_A, MDUTY); 129 analogWrite(SC_B, MDUTY); 130 131 //Turn off Motors 132 digitalWrite(MF_A, LOW); 133 digitalWrite(MF_B, LOW); 134 135 digitalWrite(MB_A, LOW); 136 digitalWrite(MB_B, LOW); 137 } 138} 139 140 141 142void onSpeedResetChange() 143{ 144 if(speedReset == true) 145 { 146 speedControl = 0; 147 speed = 0; 148 //Set the speed of the motors to 0 149 analogWrite(SC_A, MDUTY); 150 analogWrite(SC_B, MDUTY); 151 152 //Turn off Motors 153 digitalWrite(MF_A, LOW); 154 digitalWrite(MF_B, LOW); 155 156 digitalWrite(MB_A, LOW); 157 digitalWrite(MB_B, LOW); 158 } 159 160} 161 162 163void onLedChange() 164{ 165 if(led.getSwitch() == ledSwitch) 166 { 167 168 DUTY = led.getBrightness() * 2.5; 169 Serial.println(DUTY); 170 171 analogWrite(ledC1, DUTY); 172 analogWrite(ledC2, DUTY); 173 analogWrite(ledC3, DUTY); 174 analogWrite(ledC4, DUTY); 175 176 digitalWrite(ledC1, HIGH); 177 digitalWrite(ledC2, HIGH); 178 digitalWrite(ledC3, HIGH); 179 digitalWrite(ledC4, HIGH); 180 } 181 182 else 183 { 184 DUTY = 0; 185 analogWrite(ledC1, DUTY); 186 analogWrite(ledC2, DUTY); 187 analogWrite(ledC3, DUTY); 188 analogWrite(ledC4, DUTY); 189 190 digitalWrite(ledC1, LOW); 191 digitalWrite(ledC2, LOW); 192 digitalWrite(ledC3, LOW); 193 digitalWrite(ledC4, LOW); 194 } 195}
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
1/* 2 Controlled Variables through Arduino IoT Cloud 3 4 bool speedReset; 5 6 bool direction; 7 int speed; 8 int speedControl; 9 CloudDimmedLight led; 10*/ 11 12#include 13 "thingProperties.h" 14 15//Set LED Channels and variables for control 16int 17 ledC1 = 2; 18int ledC2 = 3; 19int ledC3 = 4; 20int ledC4 = 5; 21int DUTY = 0; 22bool 23 ledSwitch = true; 24 25//Set Speed Control Pins 26int SC_A = 12; 27int SC_B 28 = 8; 29 30//Set Motor Pins 31int MF_A = 10; 32int MB_A = 11; 33 34int MF_B 35 = 6; 36int MB_B = 7; 37 38//Set values for speed control 39int MDUTY = -255; 40int 41 MDUTY_N = 0; 42int Motor_Speed; 43 44void setup() { 45 // Initialize serial 46 monitor: 47 Serial.begin(9600); 48 Serial.println("Serial Monitor Initialized"); 49 50 51 pinMode(ledC1, OUTPUT); 52 pinMode(ledC2, OUTPUT); 53 pinMode(ledC3, 54 OUTPUT); 55 pinMode(ledC4, OUTPUT); 56 57 pinMode(SC_A, OUTPUT); 58 pinMode(SC_B, 59 OUTPUT); 60 pinMode(MF_A, OUTPUT); 61 pinMode(MF_B, OUTPUT); 62 pinMode(MB_A, 63 OUTPUT); 64 pinMode(MB_B, OUTPUT); 65 66 // Defined in thingProperties.h 67 68 initProperties(); 69 70 // Connect to Arduino IoT Cloud 71 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 72 73 74 if(!ArduinoCloud.begin(ArduinoIoTPreferredConnection)) 75 { 76 Serial.println("Failed 77 to Connect to wifi or UBLOX not initialized"); 78 } 79 80 /* 81 The 82 following function allows you to obtain more information 83 related to the 84 state of network and IoT Cloud connection and errors 85 the ledDUTYer number 86 the more granular information you’ll get. 87 The default is 0 (only errors). 88 89 Maximum is 4 90 */ 91 setDebugMessageLevel(2); 92 ArduinoCloud.printDebugInfo(); 93} 94 95void 96 loop() 97{ 98 ArduinoCloud.update(); 99} 100 101 102 103void onSpeedControlChange() 104 105{ 106 Motor_Speed = speedControl; 107 MDUTY = Motor_Speed; 108 109 if(MDUTY 110 > 0) 111 { 112 speed = MDUTY; 113 direction = true; 114 115 //Set 116 the speed of the motors 117 analogWrite(SC_A, MDUTY); 118 analogWrite(SC_B, 119 MDUTY); 120 121 //Power Motors to turn forward 122 digitalWrite(MF_A, 123 HIGH); 124 digitalWrite(MF_B, HIGH); 125 126 digitalWrite(MB_A, LOW); 127 128 digitalWrite(MB_B, LOW); 129 } 130 131 if(MDUTY < 0) 132 { 133 //Set 134 MDUTY_N as the absolute value of MDUTY for speed 135 MDUTY_N = abs(MDUTY); 136 137 speed = MDUTY_N; 138 direction = false; 139 140 //Set the speed of 141 the motors 142 analogWrite(SC_A, MDUTY_N); 143 analogWrite(SC_B, MDUTY_N); 144 145 146 //Power Motors to turn backward 147 digitalWrite(MF_A, LOW); 148 149 digitalWrite(MF_B, LOW); 150 151 digitalWrite(MB_A, HIGH); 152 digitalWrite(MB_B, 153 HIGH); 154 } 155 156 if(MDUTY == 0) 157 { 158 speed = MDUTY; 159 //Set 160 the speed of the motors to 0 161 analogWrite(SC_A, MDUTY); 162 analogWrite(SC_B, 163 MDUTY); 164 165 //Turn off Motors 166 digitalWrite(MF_A, LOW); 167 digitalWrite(MF_B, 168 LOW); 169 170 digitalWrite(MB_A, LOW); 171 digitalWrite(MB_B, LOW); 172 173 } 174} 175 176 177 178void onSpeedResetChange() 179{ 180 if(speedReset == true) 181 182 { 183 speedControl = 0; 184 speed = 0; 185 //Set the speed of the motors 186 to 0 187 analogWrite(SC_A, MDUTY); 188 analogWrite(SC_B, MDUTY); 189 190 191 //Turn off Motors 192 digitalWrite(MF_A, LOW); 193 digitalWrite(MF_B, 194 LOW); 195 196 digitalWrite(MB_A, LOW); 197 digitalWrite(MB_B, LOW); 198 199 } 200 201} 202 203 204void onLedChange() 205{ 206 if(led.getSwitch() == ledSwitch) 207 208 { 209 210 DUTY = led.getBrightness() * 2.5; 211 Serial.println(DUTY); 212 213 214 analogWrite(ledC1, DUTY); 215 analogWrite(ledC2, DUTY); 216 analogWrite(ledC3, 217 DUTY); 218 analogWrite(ledC4, DUTY); 219 220 digitalWrite(ledC1, HIGH); 221 222 digitalWrite(ledC2, HIGH); 223 digitalWrite(ledC3, HIGH); 224 digitalWrite(ledC4, 225 HIGH); 226 } 227 228 else 229 { 230 DUTY = 0; 231 analogWrite(ledC1, 232 DUTY); 233 analogWrite(ledC2, DUTY); 234 analogWrite(ledC3, DUTY); 235 analogWrite(ledC4, 236 DUTY); 237 238 digitalWrite(ledC1, LOW); 239 digitalWrite(ledC2, LOW); 240 241 digitalWrite(ledC3, LOW); 242 digitalWrite(ledC4, LOW); 243 } 244}
Downloadable files
Schematic
Circuit Diagram using Altium CircuitMaker (since I don't have fritzing)
Schematic
Schematic
Circuit Diagram using Altium CircuitMaker (since I don't have fritzing)
Schematic
Comments