Page 2 of 2

Re: DIY Arduino radio control for garden train

Posted: Tue Mar 12, 2024 12:34 pm
by Sam95
Installation of Arduino Nano:

There is a hole at the back of the boiler into the cabin. I increase a little bit the dimensions to have a goos access to the USB port on the Ardunio. Thus I can connect the Arduino to my computer without disassemble the loco.

The Arduino fits in the back of the boiler

Image

Image

The last part was to fit all the wires into the loco... it is not easy but possible :

Image

Re: DIY Arduino radio control for garden train

Posted: Tue Mar 12, 2024 2:40 pm
by Lonsdaler
That's an impressive conversion. Look forward to seeing it running. And thank you from me for the link to the wiring diagram software.👍🏻

Re: DIY Arduino radio control for garden train

Posted: Wed Mar 13, 2024 7:34 am
by ge_rik
Clever idea for mounting the Nano with its usb port accessible. It will save you a lot of time and effort when tinkering with the programming.

Rik

Re: DIY Arduino radio control for garden train

Posted: Wed Mar 13, 2024 9:04 pm
by Sam95
Lonsdaler wrote: Tue Mar 12, 2024 2:40 pm That's an impressive conversion. Look forward to seeing it running. And thank you from me for the link to the wiring diagram software.👍🏻
It's already filmed :



Re: DIY Arduino radio control for garden train

Posted: Wed Mar 13, 2024 9:45 pm
by Lonsdaler
Excellent. Any room for a steam loco soundcard?

Re: DIY Arduino radio control for garden train

Posted: Mon Mar 18, 2024 8:26 am
by Sam95
Lonsdaler wrote: Wed Mar 13, 2024 9:45 pm Excellent. Any room for a steam loco soundcard?
Unfortunately not in the loco, it is too tight.

Re: DIY Arduino radio control for garden train

Posted: Mon Mar 18, 2024 8:43 am
by Sam95
To finish concerning the receiver here is the code :

Code: Select all

/*
Code for locomotive RUSTY

*/

#include "SPI.h"
#include "NRFLite.h"

const static uint8_t RADIO_ID = 10;  // receiver identification
const static uint8_t PIN_RADIO_CE = 9; // connexion pin of nrf24l01
const static uint8_t PIN_RADIO_CSN = 10;

NRFLite _radio;
int POT_Value = 0;    // received bariable for the potentiometer vaule
int ARU = 0;          // emergency stop variable
int in1 = 4;          // motor rotation direction
int in2 = 3;          //  motor rotation direction
int ENA = 5;          // pwm motor
int in3 = 6;          // entry for the light
int in4 = 7;          // entry for the light
int LIGHT_Value = 0;  // variable for the light

float analogVolt = 0;  // vairable for voltage reading on pin A2 ==> 0.....1023

//packet structure including the received variables 

struct RadioPacket {
  int POT_Value;
  int ARU;
  int LIGHT_Value;
};

RadioPacket _radioData;  // variable associated to the structure


void setup() {
  pinMode(4, OUTPUT);  // pin 4 output for in1
  pinMode(3, OUTPUT);  // pin 3 output for in2
  pinMode(5, OUTPUT);  // pin 5  output for ENA PWM motor
  pinMode(6, OUTPUT);  // pin 6  output for in3 lamp
  pinMode(7, OUTPUT);  // pin 7  output for in4 lamp
  pinMode(A2, INPUT);  // pin A2 voltage reading
  Serial.begin(9600);  // serial port for debug

  if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) {
    Serial.println("Cannot communicate with radio");
    while (1)
      ;
  }
}

void loop() {
  while (_radio.hasData())  // awaiting character reading
  {
    _radio.readData(&_radioData);  // reading variable radioData including the structure with 3 variables POT_Value  ARU and LIGHT Value

    ARU = _radioData.ARU;  // writing of the value received into the declared variable
    POT_Value = _radioData.POT_Value;
    LIGHT_Value = _radioData.LIGHT_Value;

    Serial.print("Eclairage : ");
    Serial.println(LIGHT_Value);

    // **** if battery voltage is under 10.8v then stop of lights and engine to save battery, otherwise continue the programm
    // R1=10Kohm, R2=27Kohm ratio 0.270 max voltage of  4 batteries 16.8v*0.270=4.54v max at pin A2
    // (4.54x1023)/5=929
    // for the max batterie voltage we will read 929 on pin A2
    // to limit undervoltage at 2.7v per element : 10.8v  mini to read on pin A2 
    // à 10.8x0.270=2.916v
    // (2.916x1023)/5=596 to limit voltage et 2.7v per element the minium read value will be 596

    analogVolt = analogRead(A2);  // read pin A2 variable analogVolt
      Serial.println("Mesure batterie: ");
      Serial.print(analogVolt);

    if ((ARU == 1) || (analogVolt <= 596))   // if emergency stop OR battery voltage <10.8v
    {
      digitalWrite(in1, LOW);  // breake to stop motor
      digitalWrite(in2, LOW);

      digitalWrite(in3, LOW); //light off
      digitalWrite(in4, LOW);
      
      Serial.println("Etat arret urgence :");
      Serial.print(ARU);
      Serial.println("Si batterie <10.8v soit <596 alors la loco stop");
      Serial.println("Etat batterie =");
      Serial.print(analogVolt);
    }

    // ****  if no emergency stop or battery undervoltage ****

    else {
      Serial.println("Etat arret urgence : OFF");

      if (POT_Value < 450) {
        int mappedval = map(POT_Value, 450, 0, 80, 255);  //mapping of potentiometer value to the output of L298N. If pote value is between 450 and 0 the it correspond to an output from 80 to 255. This is done to apply the power to the motore . I don't map 0 to 0 otherwise the motor will not have enough power to start and ther will be a long period of time when turning potentiometer the loco will not start

        Serial.print("Reception Avant : ");
        Serial.println(POT_Value);  // affichage de la POT_Value du potentiometre
        Serial.println(mappedval);  // affichage valeur mappé pour trouver a quel valeur moteur démarre

        digitalWrite(in1, HIGH); // rotation direction of motor
        digitalWrite(in2, LOW);
        analogWrite(ENA, mappedval);  // sending of the mapped PWM value for the motor speed moteur pin 5

        delay(30);
           } 
        else if (POT_Value > 570) {
        int mappedval = map(POT_Value, 570, 1024, 80, 255);  // same as previously in th e other way

        Serial.print("Reception Arrière : ");
        Serial.println(POT_Value);  // affichage de la POT_Value du potentiometre
        Serial.println(mappedval);  // affichage valeur mappé pour trouver a quel valeur moteur démarre

        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        analogWrite(ENA, mappedval);  // commande vitesse moteur pin 5

        delay(30);
           } 
        else if (450 > POT_Value < 570) {
        Serial.print("Reception Stop : ");
        Serial.println(POT_Value);  // affichage de la POT_Value du potentiometre

        digitalWrite(in1, LOW);  // if pot value btw 450 and 570 no motor power to have a centered 0 on the potentiometer 
        digitalWrite(in2, LOW);
      }
      //********* LAMP command ************

      if (LIGHT_Value == 1) {
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
      } else if (LIGHT_Value == 0) {
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
      }
    }
  }
}
I hope the comments in the code are enough to understand it.

Re: DIY Arduino radio control for garden train

Posted: Wed Jul 23, 2025 10:33 am
by Sam95
Hello,

I want to update you about this project

My layout is working since more than 1 year with this radio control and I am not very happy with it.
The main issue is that the data transmission is not stable after few minutes of use the transmission stops so I can't control the loco. Sometimes I have to reset the controller and sometimes it is not enough. I have to run after the loco to switch it OFF and ON.

After watching the last video from Rick; it seems he has the exact same issue.

After digging a bit Internet it seems that using an Arduino pro mini natively in 3.3v can improve the reliability of data transmission and adding several capacitors could be nice too.

So I have to test all that.

Other issues are :
Poor visibility of LCD screen under the sun; selection of locomotive via menu and rotary encoder is too long. I would like to replace the system by a rotary selector with 12 positions to select the loco address. And the screen can stay to display informations only. Clearly clone the system of the rctrain transmitter.


If the system is still not enough reliable with Arduino pro mini; I saw that ESP32 can also be used with ESP NOW projet to establish direct communication in Wifi between the ESP32.

More info to follow when my aliexpress order will arrive......

Re: DIY Arduino radio control for garden train

Posted: Mon Sep 15, 2025 2:37 pm
by Sam95
Hello all,

The new version of my radio control is working now. And after several running session it seems that the connexion is stable. I didn't have any link breake yet.

So the new version is based on an Arduino Uno, with an 1.3" Oled screen so the controler is smaller and fits nicely in the hand. The 12 position rotactor to select loco addresses is very responsive I like it.

Here are 2 quick videos :
The first one is just a basic overview



The second one is a quick running session from yesterday between the rain.



If you want more info I wrote an article on my blog with the code, the stl to print the case.....
https://sam95.fr/blog/2025/09/15/t-t-2-j-version-2/

It is in french but you can have the auto translation on the top right corner.

Re: DIY Arduino radio control for garden train

Posted: Mon Sep 15, 2025 4:01 pm
by philipy
That is very impressive Sam. Well done.