Answers.Body: <p>If you don't want to touch the original hardware, you can replace the smartphone with something else like an ESP32.</p>
  <p><a href="https://github.com/corneliusmunz/legoino" rel="noreferrer">https://github.com/corneliusmunz/legoino</a></p>
  <p>This would require some programming knowledge.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-06T17:01:01.473Z
Answers.LastActivityDate: 2022-12-06T17:01:01.473Z
Answers.UserDisplayName: Bart
Answers.Body: <p>For the sake of completeness: it's possible to replace the 2-port &quot;city&quot; powered-up hub with the 6-port technic powered-up hub. That should offer more flexibility when programming.</p>
  <p>This is doable since the technic powered-up hub is 4 studs high, so it will fit within the locomotive (leaving a gap of half a stud on the front and back). It will, however, protrude about one stud over the ceiling of the locomotive (without taking into account cables and connectors!), so this is not a very aesthetic approach.</p>
  <p><a href="https://i.sstatic.net/boIZe.png" rel="noreferrer"><img src="https://i.sstatic.net/boIZe.png" alt="CAD drawing of a locomotive with the bigger hub inside" /></a></p>
  <p>It should be possible to enclose the hub (or at least make it flush with the locomotive's ceiling) by increasing the overall height of the locomotive.</p>
  <p>It should also be possible to replace the train motor with a SPIKE medium angular motor (since the latter is slightly smaller than the former), but connecting such an assembly to the train underside (and make it pivot as the train motor does) is a bit too challenging.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-06T20:41:36.343Z
Answers.LastActivityDate: 2022-12-06T20:41:36.343Z
Answers.UserDisplayName: IvanSanchez
Answers.Body: <p>I've been exploring this recently, and I wanted to share my experiences in the hopes that they are useful to others. There are a few ways this can be accomplished. Some are possible out of the box, and others require extra hardware or 3rd-party software.</p>
  <h2>Typical Package Contents</h2>
  <p>Here's the electronics we typically get out of the box with a Powered Up train set:</p>
  <ol>
  <li><a href="https://rebrickable.com/parts/28738/hub-battery-box-powered-up/" rel="noreferrer">Powered Up City hub (28738)</a>. This includes two I/O ports (used for a motor and possibly lights). The City hub does not include an accelerometer, compass, or GPS, so there is limited feedback provided. It does include a programmable RGB LED and a button.</li>
  </ol>
  <p><a href="https://i.sstatic.net/WHTub.png" rel="noreferrer"><img src="https://i.sstatic.net/WHTub.png" alt="City Hub" /></a></p>
  <ol start="2">
  <li><a href="https://rebrickable.com/parts/28740/motor-train-base-powered-up/" rel="noreferrer">Train Motor (28740)</a>. This motor is one of only two Powered Up motors that do not include rotation and distance tracking, so there is no easy way to know how far the train has traveled, but we are able to adjust its relative speed.</li>
  </ol>
  <p><a href="https://i.sstatic.net/79DB2.png" rel="noreferrer"><img src="https://i.sstatic.net/79DB2.png" alt="Train motor" /></a></p>
  <ol start="3">
  <li><a href="https://rebrickable.com/parts/22168/light-unit-led-with-cable-powered-up/" rel="noreferrer">Lights (22168)</a> may be included as headlights for the train.</li>
  </ol>
  <p><a href="https://i.sstatic.net/TN0o4.png" rel="noreferrer"><img src="https://i.sstatic.net/TN0o4.png" alt="Lights" /></a></p>
  <h2>Programming using Powered Up App</h2>
  <p>The default experience in the <a href="https://www.lego.com/en-us/service/device-guide/powered-up" rel="noreferrer">Powered Up app</a> provides a controller for the train, but we can also automate the train using a Scratch-like programming environment. This environment is powerful, but the hardware at our disposal is quite limited as a robotics platform. In particular, the included hardware has no useful sensors, which makes the environment unobservable.</p>
  <p>If we start the train from a known location, we can run it at a set approximate speed for a certain amount of time, leaving it at a new approximate location. Unfortunately, these locations will change slightly over time, as we have no way of sensing exactly far we have traveled. This might be fine if you just want the train to move for a while then stop for a while, but if you want to stop at stations, things get trickier.</p>
  <p>One solution is to induce the train to stop at a known location regularly, such as a siding. We can intentionally stall against the buffer on the siding to reset to a known position.</p>
  <h2>Adding Sensors</h2>
  <p>If we're willing to add some additional hardware, things can get much more advanced. One option is to add a color sensor to the bottom on the train:</p>
  <p><a href="https://i.sstatic.net/MIkP7.jpg" rel="noreferrer"><img src="https://i.sstatic.net/MIkP7.jpg" alt="Step 1" /></a></p>
  <p>We can connect the sensor to the port not being used by the motor.</p>
  <p><a href="https://i.sstatic.net/go7FE.jpg" rel="noreferrer"><img src="https://i.sstatic.net/go7FE.jpg" alt="Step 2" /></a></p>
  <p><a href="https://i.sstatic.net/JzFw8.jpg" rel="noreferrer"><img src="https://i.sstatic.net/JzFw8.jpg" alt="Step 3" /></a></p>
  <p>With this in place, we can modify our track by adding plates or tiles of different colors to indicate location on the track or particular actions to take. In this way, we can have the train reliably stop at stations or take other actions. Here's an example program that allows the train to wait when it sees red under the color sensor:</p>
  <p><a href="https://i.sstatic.net/PFEdO.png" rel="noreferrer"><img src="https://i.sstatic.net/PFEdO.png" alt="Program screenshot" /></a></p>
  <p>This is similar in concept to the DUPLO trains in terms of input, but we can program the colors to change behaviors in any way we'd like.</p>
  <p>We could also choose to add a different sensor, such as a distance sensor or a tilt sensor. Alternatively, we could replace the City hub with a larger hub and add multiple sensors, but this would likely require redesigning the train and would add significant cost.</p>
  <h2>Standalone Operation</h2>
  <p>One of the weaknesses of the City Hub currently is that it requires a tethered Bluetooth device, such as a phone, tablet or computer, in order to function. By flashing the custom <a href="https://pybricks.com/" rel="noreferrer">Pybricks</a> firmware to the hub, we can overcome this limitation and write programs directly to the device. This is especially useful for public displays or other use cases where Bluetooth connections may become problematic.</p>
  <p>Here is an example Pybricks program that would allow a train to run for 10 seconds and then pause for 35 using the RGB LED on the hub to indicate its status:</p>
  <pre><code>&quot;&quot;&quot;
  Simple automated train that runs for 10 seconds then pauses for 35
  
  Light transitions from red to orange to yellow as timer counts down
  &quot;&quot;&quot;
  
  from pybricks.hubs import CityHub
  from pybricks.pupdevices import DCMotor, Light
  from pybricks.parameters import Button, Color, Direction, Port, Stop
  from pybricks.robotics import DriveBase
  from pybricks.tools import wait, StopWatch
  
  hub = CityHub()
  motor = DCMotor(Port.A)
  
  while True:
      hub.light.on(Color.GREEN)
      motor.dc(60)
      wait(10000)
      motor.dc(0)
      hub.light.on(Color.RED)
      wait(15000)
      hub.light.on(Color.ORANGE)
      wait(15000)
      hub.light.on(Color.YELLOW)
      wait(5000)
  </code></pre>
  <p>I use a setup similar to the above to allow an automated train to run around my son's city without the need to have a screen nearby the whole time.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-04T22:01:53.850Z
Answers.LastActivityDate: 2022-12-05T18:35:08.050Z
Answers.LastEditDate: 2022-12-05T18:35:08.050Z
Answers.UserDisplayName: jncraton
Body: <p>I have a LEGO City train set (60336):</p>
  <p><a href="https://i.sstatic.net/wjoIZ.jpg" rel="noreferrer"><img src="https://i.sstatic.net/wjoIZ.jpg" alt="60336 Box" /></a></p>
  <p>The set includes a remote control as well as an app that can be used to control the train. Is it possible to automate the operation of LEGO City trains, ideally without needing to be tethered to a smart device to help reduce children getting distracted by a screen?</p>
  
Tags: <trains><powered-up><automation>
Title: Can LEGO City Powered Up trains be automated?
UserDisplayName: jncraton
ThreadText: # Can LEGO City Powered Up trains be automated?
  
  (Asked by: username_0 on 2022-12-04)
  
  I have a LEGO City train set (60336):
  
  [![60336 Box](https://i.sstatic.net/wjoIZ.jpg)](https://i.sstatic.net/wjoIZ.jpg)
  
  The set includes a remote control as well as an app that can be used to control the train. Is it possible to automate the operation of LEGO City trains, ideally without needing to be tethered to a smart device to help reduce children getting distracted by a screen?
  
  **Tags:** trains, powered-up, automation
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2022-12-04. Score: 57 | Accepted answer
  
  I've been exploring this recently, and I wanted to share my experiences in the hopes that they are useful to others. There are a few ways this can be accomplished. Some are possible out of the box, and others require extra hardware or 3rd-party software.
  
  ## Typical Package Contents
  
  Here's the electronics we typically get out of the box with a Powered Up train set:
  
  1. [Powered Up City hub (28738)](https://rebrickable.com/parts/28738/hub-battery-box-powered-up/). This includes two I/O ports (used for a motor and possibly lights). The City hub does not include an accelerometer, compass, or GPS, so there is limited feedback provided. It does include a programmable RGB LED and a button.
  
  [![City Hub](https://i.sstatic.net/WHTub.png)](https://i.sstatic.net/WHTub.png)
  
  2. [Train Motor (28740)](https://rebrickable.com/parts/28740/motor-train-base-powered-up/). This motor is one of only two Powered Up motors that do not include rotation and distance tracking, so there is no easy way to know how far the train has traveled, but we are able to adjust its relative speed.
  
  [![Train motor](https://i.sstatic.net/79DB2.png)](https://i.sstatic.net/79DB2.png)
  
  3. [Lights (22168)](https://rebrickable.com/parts/22168/light-unit-led-with-cable-powered-up/) may be included as headlights for the train.
  
  [![Lights](https://i.sstatic.net/TN0o4.png)](https://i.sstatic.net/TN0o4.png)
  
  ## Programming using Powered Up App
  
  The default experience in the [Powered Up app](https://www.lego.com/en-us/service/device-guide/powered-up) provides a controller for the train, but we can also automate the train using a Scratch-like programming environment. This environment is powerful, but the hardware at our disposal is quite limited as a robotics platform. In particular, the included hardware has no useful sensors, which makes the environment unobservable.
  
  If we start the train from a known location, we can run it at a set approximate speed for a certain amount of time, leaving it at a new approximate location. Unfortunately, these locations will change slightly over time, as we have no way of sensing exactly far we have traveled. This might be fine if you just want the train to move for a while then stop for a while, but if you want to stop at stations, things get trickier.
  
  One solution is to induce the train to stop at a known location regularly, such as a siding. We can intentionally stall against the buffer on the siding to reset to a known position.
  
  ## Adding Sensors
  
  If we're willing to add some additional hardware, things can get much more advanced. One option is to add a color sensor to the bottom on the train:
  
  [![Step 1](https://i.sstatic.net/MIkP7.jpg)](https://i.sstatic.net/MIkP7.jpg)
  
  We can connect the sensor to the port not being used by the motor.
  
  [![Step 2](https://i.sstatic.net/go7FE.jpg)](https://i.sstatic.net/go7FE.jpg)
  
  [![Step 3](https://i.sstatic.net/JzFw8.jpg)](https://i.sstatic.net/JzFw8.jpg)
  
  With this in place, we can modify our track by adding plates or tiles of different colors to indicate location on the track or particular actions to take. In this way, we can have the train reliably stop at stations or take other actions. Here's an example program that allows the train to wait when it sees red under the color sensor:
  
  [![Program screenshot](https://i.sstatic.net/PFEdO.png)](https://i.sstatic.net/PFEdO.png)
  
  This is similar in concept to the DUPLO trains in terms of input, but we can program the colors to change behaviors in any way we'd like.
  
  We could also choose to add a different sensor, such as a distance sensor or a tilt sensor. Alternatively, we could replace the City hub with a larger hub and add multiple sensors, but this would likely require redesigning the train and would add significant cost.
  
  ## Standalone Operation
  
  One of the weaknesses of the City Hub currently is that it requires a tethered Bluetooth device, such as a phone, tablet or computer, in order to function. By flashing the custom [Pybricks](https://pybricks.com/) firmware to the hub, we can overcome this limitation and write programs directly to the device. This is especially useful for public displays or other use cases where Bluetooth connections may become problematic.
  
  Here is an example Pybricks program that would allow a train to run for 10 seconds and then pause for 35 using the RGB LED on the hub to indicate its status:
  
  ```
  """
  Simple automated train that runs for 10 seconds then pauses for 35
  
  Light transitions from red to orange to yellow as timer counts down
  """
  
  from pybricks.hubs import CityHub
  from pybricks.pupdevices import DCMotor, Light
  from pybricks.parameters import Button, Color, Direction, Port, Stop
  from pybricks.robotics import DriveBase
  from pybricks.tools import wait, StopWatch
  
  hub = CityHub()
  motor = DCMotor(Port.A)
  
  while True:
      hub.light.on(Color.GREEN)
      motor.dc(60)
      wait(10000)
      motor.dc(0)
      hub.light.on(Color.RED)
      wait(15000)
      hub.light.on(Color.ORANGE)
      wait(15000)
      hub.light.on(Color.YELLOW)
      wait(5000)
  
  ```
  
  I use a setup similar to the above to allow an automated train to run around my son's city without the need to have a screen nearby the whole time.
  
  
  ---
  
  ### Answer by username_1 on 2022-12-06. Score: 7
  
  If you don't want to touch the original hardware, you can replace the smartphone with something else like an ESP32.
  
  <https://github.com/corneliusmunz/legoino>
  
  This would require some programming knowledge.
  
  
  ---
  
  ### Answer by username_2 on 2022-12-06. Score: 7
  
  For the sake of completeness: it's possible to replace the 2-port "city" powered-up hub with the 6-port technic powered-up hub. That should offer more flexibility when programming.
  
  This is doable since the technic powered-up hub is 4 studs high, so it will fit within the locomotive (leaving a gap of half a stud on the front and back). It will, however, protrude about one stud over the ceiling of the locomotive (without taking into account cables and connectors!), so this is not a very aesthetic approach.
  
  [![CAD drawing of a locomotive with the bigger hub inside](https://i.sstatic.net/boIZe.png)](https://i.sstatic.net/boIZe.png)
  
  It should be possible to enclose the hub (or at least make it flush with the locomotive's ceiling) by increasing the overall height of the locomotive.
  
  It should also be possible to replace the train motor with a SPIKE medium angular motor (since the latter is slightly smaller than the former), but connecting such an assembly to the train underside (and make it pivot as the train motor does) is a bit too challenging.
  
  
  ---
  
  