ARDUINO PROGRAMMING, AND INTERFACING HELP

Post all your model railway electronic problems, solutions and discoverys here.
timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Mon May 07, 2018 9:20 am

Hello every one I have decided to start a thread to help those who wish to use the Arduino in there Model Railway projects, for that anything Arduino related.
There are a number of members of this forum who are willing to help you with your Arduino projects.
What we don't know we will help you find the information that will help you.

User avatar
TimberSurf
Posts: 2158
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Distributing data via coms to multiple Arduinos

Postby TimberSurf » Mon May 07, 2018 11:41 am

OK, let me kick it off with a simple hypothetical layout, lets call it 'Theadonia'.
Although most jump in and think Arduino is capable of sitting on and interfacing with DCC, it is not the only path! There are those that are just DC and Arduino can compliment that scenario as well. (and obviously it has its uses for non track devices as well)
So for the first example, lets investigate block indication.
Initially we will ignore its use for train control and signalling, we will just concentrate on indication. (mimic panel)
If we say had 50 blocks, that would mean running 50 wires back to the mimic! (spaghetti junction). Ditto if there were 50 points, that would mean 50 or 100 wires back from the mimic to all the points!
The arduino is capable of compressing this wide number into a few wires and passing the information (status) very fast in a serial mode (one at a time, sequentially incrementing the next chunk and so on) in a short period of time (a few milliseconds).
Let's say Theadonia has 12 blocks, detected by 2 arduino's that need to be displayed as led's on the mimic.
In each Block Arduino, the status of all points is known from 6 inputs and is stored as binary as 000000 (all unoccupied).
We can compress this data into hexadecimal and send over a 2 wire communication bus.
So our six bits of information must be read as a 8 bit string (byte) for conversion to Hex.
Thus, all unoccupied (000000) = decimal 0 = 00 in hex
{Binary starts from the right}
So, first block occupied ([00]000001) = decimal 1 = 01 in hex
fifth block occupied ([00]010000) = decimal 16 = 10 in hex
sixth block occupied ([00]100000) = decimal 32 = 20 in hex
second and fourth block occupied ([00]001010) = decimal 10 = 0A in hex
So the first Block Arduino can transmit one hex chunk of data to the Mimic Arduino as hex
The second Block Arduino can transmit one hex chunk of it's data to the Mimic Arduino as hex
Each one has a different address
We can then get the Mimic Arduino to filter the addresses and poll for information from each Block Arduino in turn.
Thus it will see the status of the first Block Arduino as a hex value, then the status of the second Block Arduino as a hex value and store both values in a table.
The Mimic Arduino can then read the the table, convert the hex back to binary and allocate each individual bit (block status) to individual outputs that are in turn connected to specific individual LED's on the mimic front.
So in a similar manner to DCC, multiple Arduino's can be used on a two wire network to transmit and receive lots of data, in the form of addressable parcels of information.
There are many protocols to choose from as to how Arduino's talk to each other. I2C, SPI, UART, RS232, RS485, serial, Bluetooth, RF, CANbus, WiFi, etc. Some are only good for a few feet, other will go miles! (or thousands of miles via the internet) (yes you can run trains at home while your at work! :o :lol: )
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

Buggleskelly
Posts: 350
Joined: Mon Jan 28, 2013 5:50 pm
Location: Basildon, Essex

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Buggleskelly » Tue May 08, 2018 11:38 am

Not being very good at compiling an Arduino sketch, I spent ages to try and find a sketch on-line, even the Arduino site itself. I only wanted to operate two crossing gates via two press buttons. i.e open, closed. Never found any solution or simple help, just experts keeping the ''secrets'' to themselves. Rather like IT guys in 80's.
So being disillusioned, this being the icing on the cake, I have now dismantled my layouts, packed my stock away, and have picked up my other hobby interests. Maybe when the grandson is old enough, I'll be creating a simple DC Thomas layout for him.

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Tue May 08, 2018 12:03 pm

I’m sorry to hear about your experience with trying to find a solution to you problem, and that it caused you to abandon your Model Railway interests.
That is what are a hopping to achieve here, helping those with Model Railway interests solve there Arduino problems. As those of us here that have experience and knowledge with the Arduino mainly use these devices in our Model Railways. There is a lot of information available, but it is general and mostly not suited to our needs.
So we are only to willing to help anybody with there problems.

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Tue May 08, 2018 12:32 pm

So firstly I'll talk about getting into good habits with programming in general.
One of the most important things in any sort of programming is to properly document your code with meaningful comments. When I wrote in assembler I always but a comment on each line as it was not obvious as to what was going on. Today’s programming languages are fairly descriptive in there syntax, but still need to be well commented as when you come back later you may find you can't understand what you have done and of course helps with debugging your program.
When you name your variables and your routines give them meaningful names not just output1 but redSignalControl and there is a convention that the names start with a small letter, then each new word starts with a capital letter.
As you Develop your programme do it in manageable stages and as you get each stage working, save a copy of the programme at that stage, and start the next stage on a new copy so that you can always go back one stage when things get out of hand.
Where possible write your programme to be modular, that is each separate routine is in it's own file.
Most of my programmes have at least 3 files, the first is an information file describing about the program, the second is a configuration file where i declare all my variables and have the include statements, and the third is the main programme.
I will post an example in the next post.

Gordon H
Posts: 200
Joined: Tue Mar 14, 2006 1:08 pm
Location: UK
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Gordon H » Tue May 08, 2018 12:44 pm

Buggleskelly wrote:Not being very good at compiling an Arduino sketch, I spent ages to try and find a sketch on-line, even the Arduino site itself. I only wanted to operate two crossing gates via two press buttons. i.e open, closed. Never found any solution or simple help, just experts keeping the ''secrets'' to themselves. Rather like IT guys in 80's.

It might seem like this, but have you considered that it is entirely possible others have achieved a similar end but without using Arduinos?
Thus searching for purely Arduino based solutions could be a limiting factor.
I certainly wouldn't use one, for example, as I have many microcontroller designs available - mostly PIC based - that are more suited to the task.
Indeed I have a PIC based level crossing demonstration unit which can often be seen on the MERG stand at shows.

Buggleskelly
Posts: 350
Joined: Mon Jan 28, 2013 5:50 pm
Location: Basildon, Essex

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Buggleskelly » Tue May 08, 2018 12:53 pm

Thanks Gordon H,
Maybe I should have looked at PIC rather than Raspeberry or Arduino in the first place, as it seems more relevant for doing a specific task. I will bear this in mind when I build up more enthusiasm in the future, together with a little better planning.
Regards

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Tue May 08, 2018 1:24 pm

Gordon H wrote:I certainly wouldn't use one, for example, as I have many microcontroller designs available - mostly PIC based - that are more suited to the task.

Without getting into a debate which is the best microcontroller brand, the Arduino is by far the best supported microcontroller platform with the most visible users. There are more complete easy to use development boards that are easy to just plug you extra bits into. The PIcs are more for the hardcore electronics person where the Arduino is designed for the person who has virtually zero experience and knowledge of microcontrollers.
If all things were equal I would be using a Motorola MC68HC05 of MC68HC11,or MC68HC16 as in my opinion is they are far superior devices, but things being as they are the Arduino is my next choice.

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Thu May 10, 2018 5:32 am

TimberSurf and I are not to sure on what to present in this thread, so we do need you help in telling us what you would like, so please leave your thoughts.

What I have decided to do is do a series of Tutorials on using the Arduino in Model Railway Projects each Tutorial will be an actual project that can be used on your layout. The first project will be the proverbial "Hello World" with a twist I shall go through building a Level Crossing Flasher in several stages.

So do be able to follow these projects you will need to download and install the free Arduino IDE available from https://www.arduino.cc/en/Main/Software or at the same address you can sign up to use the online web based IDE. There are plenty of good resources that show you how to use the IDE, so I should have no need to repeat this information here.

Stage 1. I shall go through connecting the LEDs and setting up the program and getting the lights to flash in a manor that resembles the real thing. and explain the code and how parts can be changed to have control on how the lights behave.

Stage 2. I shall add 2 push button switches to the circuit, switch "A" will start the lights flashing and switch "B" will stop the lights flashing, which is single direction only the sequence of "A" is on and "B" is off

Stage 3. I shall change the code so that the circuit is by-directional which means when switch "A" start the lights flashing only switch "B" will stop the lights flashing, or when switch "B" start the lights flashing only switch "A" will stop the lights flashing.

Stage 4. I will look into various methods of actually connecting this circuit into your layout so that when a train arrives the lights flash and when the train passes the lights will stop.

Stage 5. Which may be a bit latter I will look at actually adding sound, using just the Arduino, and using external modules.

When the project is finished all the information will be available for download in the form of a PDF document.

So stay tuned as I will be starting this in the next couple of days after I have prepared everything.

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Fri May 11, 2018 4:02 am

PROJECT 1.
STAGE 1.
PART A.

Firstly I have to brake stage 1 into part A and Part B as it became larger than I was expecting.
Part A is the hardware set-up for this project, and Part B will be the software

Welcome to the first part of this project as outlined previously I an going to go through creating level crossing flashing lights using an Arduino, I will be using the Arduino Uno in this project, I will provide the corresponding information to use other Arduino models. Any reference documents or other resources I have used I will put links in the document so you can save these for later reference.
The circuit diagrams are produced in a Free program called Fritzing available from http://fritzing.org/download/

PROJECT-01_bb.png
Figure 1. Breadboard of complete circuit

PROJECT-01_schem.png
Figure 2. schematic of complete circuit

As can be seen from Figures 1 and 2, I am going to make a flasher with 4 LEDs set up as 2 sets of 2 LEDs
The Arduino can supply a DC Current on the I/O Pins of 40 mA per pin, so I need to take this in to account when working out how to connect the components up. Data sheet for Microcontroller used in the Arduino UNO
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf
PAGE 365
Section 32.1.
Absolute Maximum Ratings
Table 32-1. Absolute Maximum Ratings
Operating Temperature______________-55°C to +125°C
Storage Temperature_______________ -65°C to +150°C
Voltage on any Pin except RESET
with respect to Ground______________-0.5V to VCC +0.5V
Voltage on RESET
with respect to Ground______________-0.5V to +13.0V
Maximum Operating Voltage_________6.0V
DC Current per I/O Pin_____________40.0mA
DC Current VCC and GND Pins _____200.0mA

The circuit is fairly straight forward we have our Arduino 4 LEDs and 4 resisters, the resisters are to limit the voltage across and the current flowing through the LEDs, depending on the LEDs, used will determine the value of the resister used. I will use a current of 10mA and a voltage of 1.2 volts for each LED, so 2 LEDs on each pin is 20mA which is well with in the 40mA allowed per pin. Using the formulae of R= V/I, where R is the resister value we want to find, V is the voltage that we need to drop across the resister and I is the current we require. We are using a 5 volt supply so we need supply – voltage required = voltage across resister
5 – 1.2 = 3.8 volts we need to drop across the resister. Therefore R = 3.8/0.01 = 380 ohms nearest value is 390 ohms using the E12 series. http://www.resistorguide.com/resistor-values/#E12_series_tolerance_10 The other parameter of the resister we need to check for is the power rating in watts, which is found by Watts = Voltage x Amps, so we have 3.8 Volts x .010Amps = 0.038watts so a 1/4W resister is fine.
Resister colour codes can be found on this link http://www.resistorguide.com/resistor-color-code/

So don't forget to watch for the next part of this project.

Paul-H
Posts: 145
Joined: Fri Jun 05, 2009 3:00 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Fri May 11, 2018 8:04 am

A great thread which I shall be following closely

Circa future project how about using the Arduino to control RC servos for turnout control. With each turnout operated by a simple toggle or push button switch, don't know what the maximum number of servos the Arduino can control but as many as posible on one board would be nice.


Thanks

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Fri May 11, 2018 9:33 am

Paul-H wrote:Circa future project how about using the Arduino to control RC servos for turnout control. With each turnout operated by a simple toggle or push button switch, don't know what the maximum number of servos the Arduino can control but as many as possible on one board would be nice.

Glad this is of interest to you

I will do one on servo's as I already have implemented that on my layout There is a youtube video showing it working https://www.youtube.com/watch?v=xL76HTyo_O8

The number of servos is governed by which Arduino you have, unless you add extra circuitry The UNO and its variants that use the ATmega328 have 6 PWM pins so you can connect 6 servo's. the ATmega2560 which is used in the MEGA has 15 PWM pins so you can connect 15 servo's, Theses are the pins that have built in PWM but you can also generate software PWM. Or the other alternative is to use an external IC that specialises in PWM

User avatar
TimberSurf
Posts: 2158
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Fri May 11, 2018 11:54 am

Paul-H

For a small layout, either one large Arduino may be used or several smaller like the example in Theadonia, interconnected by the likes of SPI or IC2 (they both have to be very short distances away)
If the layout is larger (than say 8ft x 4ft) then a more robust coms is needed like a wired CANbus or a radio system/Wifi

I will be looking at a distributed system. With the cheapest Arduini, that will have 6 servos and 6 block detectors each, they can also drive local signals.
This will then integrate via coms to a mimic panel that can have switches for the points and feedback of block detection. In my case the outlying Arduino's will also drive the asymmetric braking to complete block control (automatic anti collision)
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

Paul-H
Posts: 145
Joined: Fri Jun 05, 2009 3:00 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Fri May 11, 2018 2:03 pm

Multi UNO or one MEGA will work for me

My Layout is quite small so distance shouldn't be an issue, actually its only got 11 points so one MEGA would do fine.

Paul

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

PROJECT 1 STAGE 1 PART B

Postby timbologist » Sun May 13, 2018 5:39 am

PROJECT 1
STAGE 1
PART B

Welcome all back to this project, this post I will go through the Software to run these Level Crossing Lights.
So firstly I have put up a boring video on YouTube to show the results of the program and hardware at the end of stage 1
https://youtu.be/q7faN8wXi-4
The code for the first stage is listed below I have put all the comments about the program in with the program itself, so I don’t think I need any other comments or description to go with it. To use the program you can just select code select all and paste it into the Arduino IDE.

But have a play with the program by changing the values of the delay and have a look at the results of these changes, make both of them different.
You can't break anything if you get something wrong it won't compile and you will get an error message.
So go and have fun, and any questions or problems please ask that is what we are here for.

Code: Select all

// PROGRAM SET PROJECT-01-STAGE-01
// FILE NAME PROJECT-01-STAGE-01.ino
// NEW RAILWAY MODELLERS FORUM
// ARDUINO PROGRAMMING, AND INTERFACING HELP
// PROJECT 01
// LEVEL CROSSING FLASHING LIGHTS USING AN ARDUINO
// A.R.TURNER 2018-05-13

// SET UP OUR PINS CONNECTED TO THE LEDS
// WE NEED TO SET UP OUR PIN ASSIGNMENTS OUTSIDE EITHER THE SET-UP SECTION OR THE LOOP SECTION
// SO THEY BECOME GLOBAL VARIABLES MEANING THEY CAN BE ACCESSED ANYWHERE IN THE PROGRAM
// IF WE DEFINE THEM INSIDE THE SET-UP SECTION OF THE LOOP SECTION THEY BECOME LOCAL VARIABLES
// WHICH MEANS THEY CAN ONLY BE ACCESSED WITHIN THE BOUNDS OF THAT SECTION
// IN SOME PROGRAMS YOU WILL SEE THESE ASSIGNMENT IN DIFFERENT FORMS IE
// const int redLedTopLeftBottomRight  = 2;
// OR
// int redLedTopLeftBottomRight  = 2;
// BOTH DO THE SAME THING ACCEPT THE ONE WITH THE "const" IN FRONT OF IT CREATES THE VARIABLE AS READ ONLY
// SO IT CANNOT BE CHANGED ANYWHERE IN THE PROGRAM.

const int redLedTopLeftBottomRight  = 2;  // OUTPUT PIN CONNECTED TO TOP LEFT AND BOTTOM RIGHT LED
const int redLedTopRightBottomLeft  = 3;  // OUTPUT PIN CONNECTED TO TOP RIGHT AND BOTTOM LEFT LED

// THE SET-UP PART OF THE PROGRAM IS ONLY EXECUTED ONCE WHEN THE ARDUINO IS POWERED UP OR THE
// RESET BUTTON IS PRESSED IN THIS SECTION WE SET UP OUR PINS AS OUTPUTS AND ENABLE THE SERIAL PORT
// IN THIS PROGRAM WE DO NOT USE THE SERIAL PORT AT THIS STAGE

void setup()
{
  // put your set-up code here, to run once:
 
  Serial.begin(9600);   // SET UP BAUD RATE TO BE ABLE TALK TO COMPUTER VIA USB PORT COMPUTER

  pinMode(redLedTopLeftBottomRight,OUTPUT); // SET THE PIN CONNECTED TO TOP LEFT AND BOTTOM RIGHT LED AS AN OUTPUT
  pinMode(redLedTopRightBottomLeft,OUTPUT); // SET THE PIN CONNECTED TO TOP RIGHT AND BOTTOM LEFT LED AS AN OUTPUT
}

// THE LOOP SECTION AS IT SAYS CREATES THE MAN PROGRAM LOOP THAT WILL JUST KEEP RUNNING TILL YOU TURN THE ARDUINO OFF
// SO IN THIS LOOP THERE IS 2 "digitalWrite's". THE FISRT "digitalWrite" TURNS ONE PAIR OF LEDS ON BY SETTING THE OUTPUT HIGH THAT IS TO 5 VOLTS
// THE SECOND "digitalWrite" TURNS THE SECOND PAIR OF LEDS OFF BY SETTING THE OUTPUT LOW O VOLTS.
// THE PROGRAM THEN WAITS FOR A "DELAY(750)" WHICH IN THIS CASE IS SET FOR 750 MILLISECONDS OF 0.75 SECONDS SO THE LEDS STAY ON OR OFF FOR THIS PERIOD OF TIME.
// AFTER THE DELAY THE NEXT 2 "digitalWrite's" CHANGE THE STATE OF THE 2 OUTPUTS THE LEDS THAT WERE ON ARE NOW TURNED OF
// THE PROGRAM THEN WAITS WITH A "DELAY(750)" KEEPING THE LEDS ON OF OFF FOR THIS PERIOD THE THE PROGRAM GOES BACK TO THE FIRST PAIR OF "digitalWrite's"
// AND STARTING ALL OVER AGAIN

void loop()
{
  // put your main code here, to run repeatedly:
 
  digitalWrite(redLedTopLeftBottomRight, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  delay(750);
  digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  digitalWrite(redLedTopRightBottomLeft, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  delay(750);
}


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 2 guests