Auto-Train

by ArthurClarysse in Circuits > Sensors

2367 Views, 14 Favorites, 0 Comments

Auto-Train

Poster.png

Hi there!

For my school project at MCT Howes Kortrijk, I made a self-driving train. As a child I loved playing with trains, especially Lego trains. For this project we needed to use at least three sensors, one actuator and one screen. Perfect to make a self driving train.

In this intractable you will learn how to make a self driving train step by step with the code available to use and a list with all the parts you need. Good luck!

GitHub with all my code: GitHub url

Supplies

Raspberry Pi 4B

  • We will use the raspberry pi to run the webserver and to communicate with all of our sensors

Arduino Mega

  • We need 14 analog inputs for all the sensors in the rails, 13 outputs for the led's and 8 outputs to turn the 2 5V stepper motors

ESP 32

  • We will use this micro controller with Bluetooth to drive the train.

Sensors

  • 13 LDR
  • 1 IR break beam sensor
  • 1 IR distance sensor
  • 1 Current sensor

LCD screen

  • On the screen we will display the estimated time of arrival of the train

Motors

  • 1 Generic Lego Power Function motor
  • 2 5V stepper motors to turn the two barriers

5V power supply

  • To power all of the electronics with one cable we will use a 5V 5A (25W) power supply

Circuit

Main.png
Train.png

The first image is the main circuit, at the left you see the LCD connected to the Raspberry Pi via I²C. The Arduino Mega and Raspberry Pi are connected to each other with a serial connection. The sensors are connected to the analog pins, the led's are connected to the digital pins of the Arduino. At the bottom you see the two stepper motors.

The second image is the circuit in the train itself. In the middle is the ESP 32 , left of it you see the motor which is controlled with a TIP 120 transistor. You also see the current sensor and the IR distance sensor. Left of it you can see the battery and the four head lights.

Database Design

DB.png

I collect all the train data, when the train departs, from which platform, when the estimated time of arrival is and what the actual time of arrival is. If the train was too late, I also save the reason why. Above this paragraph you can see the ERD of my BCNF database.

Configure the Raspberry Pi

RPI.png

To use our Raspberry Pi as a webserver we first need to install some software.

  • The OS

I use Raspberry Pi OS as the operating system. You can install it by downloading the ISO file from here and make a bootable SD-card with Rufus

  • Remote SSH for visual Studio Code

Because we need to code a lot on the Raspberry Pi it is very useful if we can do it in Visual Studio Code on our main pc. You can download and install the extension via the built-in extension tab or by installing it from here.

  • Enable the I²C-bus

If we want to use a I²C display we need to enable the I²C bus. You can do that by typing

sudo raspi-config

in the command line, choosing Interfaces and then enabling the I²C-bus.

  • Install MariaDB

If you want to host your database on the Raspberry Pi, you need to install MariaDB

sudo apt-get install mariadb-server
  • Python packages

To run the webserver, you need to install these packages with Pip:

  • Flask
  • Flas-CORS
  • Flas-MySQL
  • Flask-SocketIO
  • Python-SocketIO
  • Gevent
  • Gevent-Websocket

Or if you are using my GitHub code simply type

pip install -r requirements.txt

in the command line.

  • Reboot

The last step is to reboot the Pi, that way everything is installed properly and we can start to code.

Make the Circuits and Test

_CLA8882-Edit.jpg

Now we want to make all of the circuits. We will start with the Raspberry Pi because it is the anly thing that works alone. After that we well make the circuit with the Arduino Mega and connect it via the RX and TX pins to the
Pi. At last we will make the train circuit. After uploading the code to the ESP 32 we can connect the RPI and the ESP32 via Bluetooth. Now we can test everything.

Does the train drive? Great you've connected everything perfectly!

Now we can build the website.

Makerskills

Hout.jpg
Hout2.jpg
Hout 3.jpg

It is time to make the frame to put the tracks on. Above the paragraph you can see the dimensions of the frame. It is very simple to make, you only need a saw and a screwdriver. The top layer is MDF supported by wooden beams, that way the MDF remains stable.

After everything is build, you need to paint it. I used spray paint but any white paint will do the job. Just be careful that the top layer has a good primer otherwise the MDF will suck in all the wet paint.

Build the Website

Site.png

To build the responsive web app, I used HTML, CSS and JavaScript.

All the basic elements of the page are written in HTML, they are then styled with CSS and at the end JavaScript makes the site dynamic.

The front end of our website communicates via the rest API and Socket IO that I wrote on the flask web server.

Final Step

Final.jpg

The final step is to put everything together. Put the Raspberry Pi, the power supply and Arduino mega at the bottom of the frame. On top of the frame, you put the train tracks with the LDR's and the IR break beam sensor.

Be careful to leave enough room for the train between the IR break beam sensor.

After some final testing is project done! Congratulations!