Model train speed controller issue

I am busy making a model train speed controller. The controller when complete should be able to change the speed of the train and should be able to change the direction of the train.

To control the speed I am using a 1k/ohm potentiometer connected to A0 and PWM 6 connected to 1k/ohm resistor that is connected to the base of TIP120.

The schematic below shows a relay... it is my attempt to show a 2 way toggle switch that can switch the polarity of the supply.

According to the simulation it is suppose to work but when I built it, it does not seem to work. Does anyone have any ideas?

Ps see code below:

int hold1 = 0; //hold1&2 are used to compare to the current input readings.
int hold2 = 0; //if the input and hold do not match then the speed needs to change.
int T1Reading; 
int T2Reading; //T_Readings holds the input readings
int T1 = 6;
int T2 = 5;    //T values indicate the pwm outputs
int S1 = A0;
int S2 = A1;   //S values indicate the input sensor values
int Value1;
int Value2;    //Value_ indicates the speed that the train will be traveling.

void setup() {
  
  Serial.begin(9600);
  Serial.println("Let's start.");
  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  
}

void loop() {
  T1Reading = analogRead(S1);

  if(T1Reading != hold1){            // check if input changed from last reading
    Value1 = getNew(T1Reading);      // calls getNew() to calculate new speed
    hold1 = T1Reading;               // changes the hold value to new reading
  }
  Serial.print("Analog in: ");
  Serial.print(T1Reading);
  Serial.print(" Output value: ");
  Serial.println(Value1);
  analogWrite(T1,Value1);

  T2Reading = analogRead(S2);

  if(T2Reading != hold2){            // check if input changed from last reading
    Value2 = getNew(T2Reading);      // calls getNew() to calculate new speed
    hold2 = T2Reading;               // changes the hold value to new reading
  }
  Serial.print("Analog in: ");
  Serial.print(T2Reading);
  Serial.print(" Output value: ");
  Serial.println(Value2);
  analogWrite(T2,Value2);
  
}


int getNew(int newValue){                //PWM output is 0 to 255
  int temp = map(newValue,0,1023,0,100); //The train can't move long before the output is at 0% so I am changing the scale so that
  Serial.print("Nob is at ");            //if the input is less than 5% then output is 0 else its what ever %(6 to 100) + 155.
  Serial.print(temp);                    //eg. 7% is 7 + 155 = 162 output && 99% is 99 + 155 = 254 output.              
  Serial.println("%!");        
  if(temp > 7){                          
    return (temp + 155);                 
  } else {
    return 0;
  }
}

Hi, welcome to the forum.

Could you do a few things ?
Edit your post and put the sketch between lines with three backslash-single-quotes or use the </> button.

```
Your sketch
```

It is hard to understand how it is connected.
I see that you power a 5V relais with 12V ? A relais can not be controlled with a PWM signal.
To control a motor and its direction, a H-bridge is used. There are motor driver modules that are a H-bridge. I think that Tinkercad has a H-bridge chip as well.

A normal motor is called a "brushed DC motor". Pololu has a nice set of driver modules, they look like this: https://www.pololu.com/category/11/brushed-dc-motor-drivers

There is a possibility to make a public link for your project at Tinkercad. Don't just copy the URL of your project, you need that specific public sharing link.

Modern traincontrollers use calibration, regarding speed. Speed is measured at desired/selected PWM values. Interpolation is used for the PWM values that don't have a measured value.

This is a 30 year old set that he recently received, does that help?

Apologies, I was using tinkercad and they did not have a toggle switch, the relay was the closest match to indicate the wiring. I attached a pic of the 2way toggle switch. In the center position the common is off top position the positive and negative will flow one way and when toggled to the bottom negative and positive will flow in the opposite direction to the motor.

The trains? If so, no problem. My oldest units are some 55 years old.

If the simulation works and not the circuit, it's fairly obvious where the problem lies. Please post images of your wiring.

You are making something with Tinkercad that is not even close to a real driver.
Tinkercad has the L293D. That driver chip is outdated and has too many disadvantages, but perhaps you can use it in Tinkercad for now, instead of a driver module.

Hi, @Cois_za
Can you DRAW your circuit please, with pen(cil) and paper.
Fritzy pictures show basically nothing of pin names or labels.

Draw proper schematic and label components and pin names, and you ca draw your proper DPDT switch.

Can you post a picture of your project so we can see your component layout.

The write a basic pot input PWM output code and get it working.
Doing all your code in cad before using real world components is flawed as you have found.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Sorry Tom for the late response, I had a hectic week. Please see circuit below, I am not known for my drawing skills so I will settle for it being legible.
I


I used this arduino nano, NPN TIP120, 1k/Ohm potentiometer and
toggle switch

Below is the physical board and toggle switch.


Hi,
Thanks for the info.
Have you got the gnd of the Nano connected to the gnd of the 12V rail supply, you need this as the signal from the Nano is with respect to the Nano gnd.

To conduct that current through the Darlington bases you need a return link through the Darlington emitters, so the rail supply gnd (0V).

What is your 12V supply?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:
PS. You can also reduce the load on the Nano 5V regulator by using 10K pots rather than 1K.

Yes the gnd's are linked.

You should have reverse biased diodes across the transistors, to conduct the back EMF that you get from inductive loads like train motors.

I forgot to mention there is a doid between 12v going to motor and the ground going to transistor!
:man_facepalming:

Is it working, or not working now?

I did this at the start just forgot to draw them in the sketch...

Does the project work now?