Venderwel

Help needed with Arduino ESP32 and 9v train

Recommended Posts

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 by Venderwel

Share this post


Link to post
Share on other sites

Hi @Venderwel

Li'l more input required :pir-laugh:

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 :pir-wink:)

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

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
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 by Ludo
ENA pin not defined as output.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.