Model railway - Con...
 
Notifications
Clear all

Model railway - Controlling servo SG90 with ATTiny85?

18 Posts
8 Users
7 Likes
8,866 Views
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

Hello,

I am relatively new to Arduino etc. I am searching for a cheap way to control SG90 servos for points (turnouts) for my model railway. I build in sections/modules each about 2 feet by 4 feet. I like to use ON/On toggle swiches decentralized to each section/module. Normally there is one or two servos pr section/module. Can I use the ATTiny85 solely to control one or up to three servos since there are 3 PWM pins?

I am aware that I can use an arduino UNO (optionally with PCA9685) centrally located, but there are 8 sections containing servos and other electronics so I want to avoid too many electrical connections. Also this is a Fremo consept which means frequent put together and take apart of the sections.  The control of the trains are DCC and walk along concept for driving the trains.  The toggle will be placed in recesses in the fascia near each point. Have a look here:

My project


   
Inq reacted
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1075
 

Hi @keknor 

Wow, that is an impressive model railway (and some very impressive surroundings you are building it in as well).

You SHOULD be able to control three servos with an ATTiny85, although doing this will use up most of the internal registers so you won't be able to do a lot of other timer-related things. But for simple applications, it should be OK.

You also might find that you might have better results pulsing the servos manually instead of using the Arduino Servo Library, the ATTiny85 runs at a reduced clock frequency so doing it manually can give you better control. You can find several examples of this on the internet, one of them here at RobotShop.

This train layout definitely needs a video!

?

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
keknor reacted
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

@keknor

Just dropped by to say nice job on the model railroad.  Fantastic detail to be sure.  I've always wanted to build one but can never find the time.

DroneBot Workshop Robotics Engineer
James


   
keknor reacted
ReplyQuote
(@toolmaker)
Member
Joined: 4 years ago
Posts: 10
 

Hi @Keknor,

I am also a model trainiac. Great job on the scenery you've done. I also used SG90's for moving points on our new club layout being constructed. Ours is N guage T-Trak modules and everyone takes a module home and works on it doing various particular jobs they are gifted at. If you scan through Bill's tutorials you will find one that makes "mini micro-controllers" using the AT328 chip with a 16mhz crystal etc and you can program it with Arduino to do your bidding then permanently fix it on your layout, At a few dollars each it wont break the bank to have one on each of our modules that contain electronics like flashing leds at level crossings etc. You will find the guys here really helpful too so good luck with your endeavours. I will keep track of how you progress.

Cheers.


   
keknor reacted
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

@robo-pi

 


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

@dronebot-workshop

Thank you for the answer.

I will try it as soon as I get the ATtiny85 boards.

I have a UNO board and uploaded the Servo h sketch to it. It worked. Also when I changed the POT to an ON/ON swicth altering the angle between 45 and 135 deg which is what I need  for my points. But when I changed to a NANO It did not work. I got this message:

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00

How can I solve this?


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

@dronebot-workshop

Here is an old documentary of the real railway a few years before it closed down in 1960


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

@keknor

It's nice to have a video like that to model from.  You can get a lot of good ideas.  You should have little model men pushing the engine around when it's on the turntable. 😎 

DroneBot Workshop Robotics Engineer
James


   
keknor reacted
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @keknor

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00

This looks like some serial connection errors during the upload to the board.  You can try to disconnect it, close the IDE, reopen, re-plug the board and check the COM port comes on ok, then re-upload

Eric


   
keknor reacted
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

@robo-pi

Yes, and fortunately a small part has been saved and is now a preserved heritage railway/museum

https://mia.no/tertitten


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  
Posted by: @keknor

Hello,

I am relatively new to Arduino etc. I am searching for a cheap way to control SG90 servos for points (turnouts) for my model railway. I build in sections/modules each about 2 feet by 4 feet. I like to use ON/On toggle swiches decentralized to each section/module. Normally there is one or two servos pr section/module. Can I use the ATTiny85 solely to control one or up to three servos since there are 3 PWM pins?

I am aware that I can use an arduino UNO (optionally with PCA9685) centrally located, but there are 8 sections containing servos and other electronics so I want to avoid too many electrical connections. Also this is a Fremo consept which means frequent put together and take apart of the sections.  The control of the trains are DCC and walk along concept for driving the trains.  The toggle will be placed in recesses in the fascia near each point. Have a look here:

My project

Hello again.

Now I have wired up a Nano board  with an ON/ON swich in stead of a POT and adjusted the turn of the servo to -45/+45 deg. It works fine on the test table. But when I go to my train layout and attach my installed servo for my turnout(point), there are buzzing and some erratic tiny back and forth movement. I  think I know why: To swich the polarity of the frog in the turnout , the servo arm pushes a Tact Swich (KW11-3Z 5A 250V) microswich 3PIN Buckle from WISH. The arm of this swich pushes back on the servoarm because of its springy arm. The servo put a force the opposite way trying to reach its destination. I think the solution can be to shut off the power to the servo when it have reached the predefined angle.

What do you think? How will the addition to the code be?

Here is my sketch:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 45, 135); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(25); // waits for the servo to get there
}

This post was modified 4 years ago by keknor

   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@keknor

Hello, that would mean using myservo.detach() and myservo.attach(9) somewhere in your loop, but I see 2 issues to manage :

  • when you re- attach(), the servo will do a "brutal" move
  • you need to be sure the servo has set your turn point+polarity correctly before you detach()

Another approach could be to use some flexible mechanical "transmission" so the servo doesn't gett too much effort to produce

Eric


   
ReplyQuote
keknor
(@keknor)
Member
Joined: 4 years ago
Posts: 27
Topic starter  

@zeferby

This is interesting. But I have had no luck in trying to put the command into the sketch. I am afraid I am not so good at writing sketches yet. Help here will be appreciated.  Mechanically I think when the servo is shut off current at its predefined destination, it will have enough friction to hold against the feather force from the Tact switch.

Another thought is to change the polarity to the frog electronically The current through the rails is ca 15V AC.  Maybe two transistors?


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@keknor

You could try something like this...Warning ! Not tested !

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup() {
}

void loop() {
  val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 45, 135); // scale it to use it with the servo (value between 0 and 180)
  myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(val); // sets the servo position according to the scaled value
  delay(25); // waits for the servo to get there
myservo.detach();
}

I'm pretty sure you'll have to increase the 25ms delay to a bigger value

 

Eric


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@keknor

Also, this could be an idea for controlled moves of the servos :

 

Eric


   
ReplyQuote
Page 1 / 2