{"id":720,"date":"2026-10-29T14:58:42","date_gmt":"2026-10-29T05:58:42","guid":{"rendered":"https:\/\/vandermeij.familyds.com\/wordpress\/?page_id=720"},"modified":"2026-11-01T15:10:48","modified_gmt":"2026-11-01T06:10:48","slug":"train-controller-hardware","status":"publish","type":"page","link":"https:\/\/vandermeij.familyds.com\/wordpress\/?page_id=720","title":{"rendered":"Train Controller: Hardware"},"content":{"rendered":"<p><a href=\"https:\/\/vandermeij.familyds.com\/wordpress\/?page_id=717\">\u2190 Back to Train Controller<\/a><\/p>\n<p dir=\"ltr\">This page describes the electronics of the train controller. It is built around a Raspberry Pi 2B and two custom circuit boards that connect it to the motor, the sensors and the track.<\/p>\n<h2 dir=\"ltr\"><strong>The two circuit boards<\/strong><\/h2>\n<p dir=\"ltr\">The two boards plug onto the Raspberry Pi like \u201chats\u201d.<\/p>\n<p dir=\"ltr\">The Bottom Hat converts the Raspberry Pi\u2019s 3.3 V signals to 5 V with BC337 transistors. It also carries the overcurrent protection and the current measurement (see below).<\/p>\n<figure id=\"attachment_751\" aria-describedby=\"caption-attachment-751\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-751 size-large\" src=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB-1024x640.png\" alt=\"Picture of the Bottom Hat circuit board\" width=\"1024\" height=\"640\" srcset=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB-1024x640.png 1024w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB-300x188.png 300w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB-768x480.png 768w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB-1536x960.png 1536w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_PCB.png 1689w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-751\" class=\"wp-caption-text\">The Bottom Hat: level shifting from 3.3 V to 5 V, overcurrent protection and current measurement.<\/figcaption><\/figure>\n<p dir=\"ltr\">The Top Hat holds the 12 V circuitry. It converts the 5 V signal to 12 V and drives an IRF4905 P-channel MOSFET, which feeds the motor directly. A switching regulator on this board makes 5 V from the 12 V supply, and a relay powers one of the two tracks.<\/p>\n<figure id=\"attachment_752\" aria-describedby=\"caption-attachment-752\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-752\" src=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB-1024x797.png\" alt=\"Alt text: Picture of the Top Hat circuit board\" width=\"1024\" height=\"797\" srcset=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB-1024x797.png 1024w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB-300x234.png 300w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB-768x598.png 768w, https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_PCB.png 1356w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-752\" class=\"wp-caption-text\">The Top Hat: the 12 V circuitry with the motor-driving MOSFET, the 5 V regulator and the track relay.<\/figcaption><\/figure>\n<h2 dir=\"ltr\"><strong>Controlling the motor speed<\/strong><\/h2>\n<p dir=\"ltr\">The speed is set with PWM (pulse-width modulation) from a hardware PWM pin of the Raspberry Pi (GPIO 18). A 100 k\u03a9 pull-down resistor keeps that pin low while the Raspberry Pi starts up, so a train cannot move by accident.<\/p>\n<p dir=\"ltr\">The motor starts turning only when it receives about a third of the full voltage, roughly 4 V on the 12 V supply. The slowest running speed is therefore set just above that point. The controller uses a PWM frequency of 100 Hz for slow running and 5,000 Hz for normal speed.<\/p>\n<h2 dir=\"ltr\"><strong>Protection<\/strong><\/h2>\n<p dir=\"ltr\">Overcurrent protection is built entirely in hardware. A latch circuit on the Bottom Hat watches the current through a small shunt resistor. If the current gets too high (the trigger level is set with a trimmer), the circuit pulls the PWM signal to ground and the motor stops. This works independently of the software. In addition, an MCP3008 converter measures the motor current, and the dashboard shows the peak and effective values.<\/p>\n<h2 dir=\"ltr\"><strong>Sensors, track and train identification<\/strong><\/h2>\n<ul dir=\"ltr\">\n<li>IR dots (infrared sensors) detect the slow-down zone and the stopping points at Platform A and Platform B. Reed contacts, switched by magnets under the train, were tried earlier but did not work: the magnets were too thick to stay attached, and thinner ones are not available.<\/li>\n<li>A stepper-driven (or relay-driven) turnout routes the train to one of the two platforms.<\/li>\n<li>A scanner identifies each train by a code on a sticker under the train. The scanner can read both barcodes and QR codes, and I am still testing which works best. There is little space between the wheels of the train, so the code has to be small.<\/li>\n<\/ul>\n<h2>Circuit diagrams<\/h2>\n<p>The full circuit diagrams of both boards are available as PDF files. They stay sharp when you zoom in.<\/p>\n<ul>\n<li><a href=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Bottom_HAT_Schematic-1.pdf\">Bottom Hat schematic (PDF)<\/a>: the circuit diagram of the Bottom Hat, with level shifting, the overcurrent latch and the current measurement.<\/li>\n<li><a href=\"https:\/\/vandermeij.familyds.com\/wordpress\/wp-content\/uploads\/2026\/11\/Top_HAT_Schematic-1.pdf\">Top_HAT_Schematic<\/a>: the circuit diagram of the Top Hat, with the motor driver, the 5 V regulator and the track relay.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u2190 Back to Train Controller This page describes the electronics of the train controller. It is built around a Raspberry [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":717,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"disabled","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"disabled","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-720","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=720"}],"version-history":[{"count":8,"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/720\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/720\/revisions\/756"}],"up":[{"embeddable":true,"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/717"}],"wp:attachment":[{"href":"https:\/\/vandermeij.familyds.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}