Venderwel Posted December 12, 2022 Posted December 12, 2022 (edited) Hi all, recently got a 9v train system. The Lego World City 4561, a compleet used set, all the bricks are still there! But now I want to use Arduino to be able to control it. And I need a little help setting it up. I found this website https://arduinolegotrains.wordpress.com/ And it uses this piece of code: //This sketch makes a classic 9V or 12V train move backwards and forwards on //the track for a fixed amount of time. int IN1=22; // train motor pin 1 int IN2=24; // train motor pin 2 int ENA=10; // train motor PMW void setup() //run once at startup { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); analogWrite(ENA, 110); } void loop() // run repeatedly from top to bottom { digitalWrite(IN1,LOW); // the train will move forward digitalWrite(IN2,HIGH); delay(250); // lock up for 0.25 seconds digitalWrite(IN1,LOW); // the train will stop digitalWrite(IN2,LOW); delay(2000); // lock up for 2 seconds digitalWrite(IN1,HIGH);// the train will move in reverse digitalWrite(IN2,LOW); delay(250); // Lock up for 0.25 seconds digitalWrite(IN1,LOW); // the train will stop digitalWrite(IN2,LOW); delay(2000); // lock up for 2 seconds } Now I am not a complete idiot, but I do feel like one. I know it has to do with the analogWrite but I can't get it to work on a AZ Delivery Wroom esp32 board. Maybe I don't know how to google anymore, but if someone here can help me a little in the right direction, that would be awesome! Edited December 12, 2022 by Venderwel Quote
XG BC Posted December 12, 2022 Posted December 12, 2022 what hardware do you use to drive the motor? Quote
Toastie Posted December 12, 2022 Posted December 12, 2022 Hi @Venderwel Li'l more input required Hardware: So, you have the ESP board up and running in the Arduino IDE. And you have 4561. This one has a 9V motor, usually powered by the rails. Where is your ESP coming in? Does it power the rails? That won't work, I believe - the ESP cannot drive the current you need for 4561. Or does it sit on the train and has its outputs connected to the motor? Again, I don't know, but the ESP Wroom board may not be ready to supply the current you need. The ESP runs on 3.3V ... even if the outputs were able to drive the current, the voltage would be really insufficient to do anything ... Software: I don't get the setup (which doesn't mean anything, I don't get a lot of stuff programming-wise ) However: Pins 22 and 24 are declared as outputs. Are these connected to the motor/track? Why do you need pin 10 at all? You never use that in the loop ... As said, more input required ... Best, Thorsten Quote
dr_spock Posted December 13, 2022 Posted December 13, 2022 Can you put up pictures of your setup and/or circuit board and/or diagram onto an image hosting site and deep link them here? The AnalogWrite is to provide a PWM signal to the motor driver IC. 110 divided by 255 is 0.43. Run the train motor at 43% power. Quote
Ludo Posted December 18, 2022 Posted December 18, 2022 (edited) On 12/12/2022 at 8:37 PM, Venderwel said: Hi all, recently got a 9v train system. The Lego World City 4561, a compleet used set, all the bricks are still there! But now I want to use Arduino to be able to control it. And I need a little help setting it up. I found this website https://arduinolegotrains.wordpress.com/ And it uses this piece of code: //This sketch makes a classic 9V or 12V train move backwards and forwards on //the track for a fixed amount of time. int IN1=22; // train motor pin 1 int IN2=24; // train motor pin 2 int ENA=10; // train motor PMW void setup() //run once at startup { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); analogWrite(ENA, 110); } void loop() // run repeatedly from top to bottom { digitalWrite(IN1,LOW); // the train will move forward digitalWrite(IN2,HIGH); delay(250); // lock up for 0.25 seconds digitalWrite(IN1,LOW); // the train will stop digitalWrite(IN2,LOW); delay(2000); // lock up for 2 seconds digitalWrite(IN1,HIGH);// the train will move in reverse digitalWrite(IN2,LOW); delay(250); // Lock up for 0.25 seconds digitalWrite(IN1,LOW); // the train will stop digitalWrite(IN2,LOW); delay(2000); // lock up for 2 seconds } Now I am not a complete idiot, but I do feel like one. I know it has to do with the analogWrite but I can't get it to work on a AZ Delivery Wroom esp32 board. Maybe I don't know how to google anymore, but if someone here can help me a little in the right direction, that would be awesome! Hi, As far I can see in the code, you need a H bridge to connect between your Arduino and the track. You can't connect you motor straight to the Arduino pins. Most use this kind of H bridge (L298N) - see picture: https://dronebotworkshop.com/dc-motors-l298n-h-bridge/ - scroll down to find the connections. http://synacorp.my/v3/121-large_default/arduino-l298n-dc-motor-driver-dual-h-bridge.jpg the AnalogWrite(ENA,110) sets the speed for the motor. the IN1 and IN2 are connections on the H bridge module and need te be connected with the right pins of your ESP. I dont have a ESP, but check if the pin numbers in the script correspond with the ones on your ESP board. Increase the value (110) to run faster, decrease it to run slower. when checking the code on the above link, i notice that your ENA pin is not set as output, can be your problem. see: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ for more info I hope this info can help you solve your problem. Best regards. Edited December 18, 2022 by Ludo ENA pin not defined as output. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.