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 )

PROJECT 02 TRACK POLARITY INDICATOR

Postby timbologist » Fri May 25, 2018 4:36 am

Nothing like being side tracked, had a PM from a member regarding what to buy.
But they also asked about using an Arduino to check the track voltage polarity on a DC system as to which is positive and which is negative.
For the Arduino this is a pretty easy task, but would be a massive overkill, but if you add getting the actual track voltage and display this on an LCD or 7 segment display then it would become another nice little project that would be a handy tool to have on hand.
And with a PWM system could give the frequency, the duty cycle, the average voltage, the peak voltage, the min voltage, all on an LCD screen.
So I shall put this on the list of projects, as I could use one myself.

So with the above question I have put together a small quick simple device to do the job and put it together as Project 02 TRACK POLARITY INDICATOR
I have attached the circuit and some photo's of a simple device to check the polarity of the track voltage, using 2 red LED's and 2 green LED's, and 2 resisters. It is assembled on a piece of circuit board so the LED's sit over the rails the green LED indicates the positive rail and the red indicates the negative rail.


PROJECT-02_schem.png
PROJECT 02 TRACK TESTER SCHEMATIC
PROJECT-02_schem.png (34.87 KiB) Viewed 498 times

PROJECT-02_bb.png
PROJECT 02 TRACK TESTER BREADBOARD

MVC-412F.JPG
PROJECT 02 TRACK TESTER ONE WAY

MVC-411F.JPG
PROJECT 02 TRACK TESTER OTHER WAY

User avatar
RAFHAAA96
Posts: 494
Joined: Tue Feb 19, 2008 1:39 pm
Location: Cyprus

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby RAFHAAA96 » Fri May 25, 2018 7:07 pm

I have a similar toggled arrangement using bicolour 3-legged red/green leds for two purposes on my DCC track.

1. Is used to indicate which way my points are selected. Installed as red/green or green/red traffic lights at each turnout location.

2. Is set to indicate the ‘polarity’ of my turntable deck. One dual red/green led at each end of the TT deck. Green to be on at the end in use, else a DPDT switches the bed polarity.

in each case a green indicates this way is OK and a red indicates this way is wrong.
Rob
RAF Halton Brat - 96th Entry
http://www.halton96th.co.uk

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 » Sat May 26, 2018 3:14 am

RAFHAAA96 wrote:I have a similar toggled arrangement using bicolour 3-legged red/green leds for two purposes on my DCC track.

Thank you for you input for the use of multi-coloured LED'S.
Some of you may ask why cannot I use a multicoloured led for the track polarity indicator.
Referring to the attached schematic that the multicoloured LED the cathodes of the 3 LED's are joined together internally making this LED a COMMON CATHODE. Because of this internal connected we are unable to use them in our particular application as we need to connect ANODES to CATHODES

Which means to be able to use multicoloured LED's the CATHODE is connected to ground and we apply the positive to the individual ANODES.

When compared to the schematic of the track polarity indicator each pair of leds has the ANODE connected to the CATHODE of the other LED in the pair.
So depending on the polarity of the voltage will determine which LED in the pair is lit.
Because multicoloured LED's are internally connected we are unable to use them in our particular application

PLEASE NOTE DID NOT HAVE A SYMBOL FOR JUST A BI-COLOURED LED SO USED A TRICOLOUR INSTEAD

BI-COLOUR-LEDS_schem.png
TRICOLOURED LEDS SCHEMATIC

BI-COLOUR-LEDS_bb.png
TRICOLOURED LEDS COMPONENT LAYOUT

User avatar
RAFHAAA96
Posts: 494
Joined: Tue Feb 19, 2008 1:39 pm
Location: Cyprus

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby RAFHAAA96 » Sat May 26, 2018 8:00 am

Yes I used common cathode 3-legged red/green leds for my quoted projects.

For DCC in loco use I use red/white common anode leds.

I haven’t tried cross connecting them per your dc track indicator schematic to see if they would work together.
Rob
RAF Halton Brat - 96th Entry
http://www.halton96th.co.uk

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 31, 2018 1:54 am

Well I'm back again as threatened.
Today will start discussing the various methods to connect the Level Crossing Flasher up to the layout.
There are 2 main categories which I shall call intrusive and non-intrusive. Firstly intrusive is devices are those that are required to be placed in line of the track power. These include Voltage Differential Sensors and Current Sensors.
The non-intrusive are those sensors that do not connect into the track wiring. These include Reed Switches, Hall Effect Switches, Inductive Proximity Switch, IR LEDS Sensors, LDR Sensors.
Each sensor has it's advantages and dis-advantages, that the user has to decide on which best suits there particular situation.
To make things easier for those that are new to Electronics and the Arduino, I will mainly discuss ready made modules that are cheaply available on Ebay that you just use jumper wires and plug them together like a Lego set.

So the first one is the Magnetic Reed Switch which is very simple in operation, I will talk about the Normally Open Reed Switch here, there are other types available.
When a magnet is placed near the reed that is the correct polarity the reed will move and close the circuit.
Advantages are they are cheap and simple ( if you buy the reads without the rest of the PCB as shown in the picture.
Disadvantages you need to remove some of the sleepers between the tracks to mount them, which may not look very good on your particular layout. And you need to mount a magnet on the underside of all the Loco's and any other rolling stock that may be required for sensing I.E. the guards or last carriage for end of train sensing. The small rare earth magnets are reasonably cheap a few cents each and some are about 1mm thick and 2mm diametre so are very unobtrusive.

The second sensor works in a similar manor as the Magnetic Reed Switch and that is the Hall Effect Sensor which again requires a magnet to trigger it.
Advantages when compared to the Magnetic Reed Switch you only need a small hole between the tracks so are much easier to hide, there are a lot faster than the Reeds, and the sensitivity can be altered.
Disadvantages are the same as the Magnetic Reed Switch you are required to mount magnets under your Loco's and rolling stock.

The third sensor is a similar device to the Hall Effect Sensor called an Inductive Proximity Switch that can sense metallic objects and does not need a magnet, they can come in 2 wire 3 wire and 4 wire typse.
Advantages do not require a magnet.
Disadvantages need to add metal to plastic rolling stock, the sensitivity is not as good as the Magnetic sensors, and need to be closer to what is being sensed, they are larger than the Hall Effect Sensor.

That shall do for today, hope I have explained things well enough for you all,.
Don't forget any questions please ask.

And if you are not a member please join then you can post questions, and enjoy this friendly community.

REED-SWITCH.jpg
MAGNETIC REED SWITCH

HALL-EFFECT.jpg
MAGNETIC HALL EFFECT SENSOR
HALL-EFFECT.jpg (27.44 KiB) Viewed 402 times

INDUCTIVE-PROXIMITY-SWITCH .jpg
INDUCTIVE PROXIMITY SWITCH

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 » Thu May 31, 2018 8:28 pm

Hi Timbologist
Can you explain why a reed switch needs a a dozen discrete components including an I.C. for what is just a switch and what the pot is for?
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

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 Jun 01, 2018 4:04 am

TimberSurf wrote:Hi Timbologist
Can you explain why a reed switch needs a a dozen discrete components including an I.C. for what is just a switch and what the pot is for?

At least I know somebody is keeping an eye on me. I picked this module as it had a nice picture and there was no need to add any external components to make it work I.E. pull-up resister. I did not take any notice of the rest of the components.
But seeing you asked here is my answer.

This appears to be a universal board meant for optical sensors – with a Reed Switch in place of a photo-transistor, temp sensor, or some other sort of sensor.
Looking at the schematic, the circuit has a LED for letting us know there is power, and an LED on the output of the LM393 that turns on when the Reed Switch is closed.
The rest of the components are not really needed but will help condition the signal from the Reed Switch,The Reed Switch is connected to the sensor terminals, the 10k resister acts as a pull-up so the input to the LM393 is not floating, the 104 capacitor across the Reed Switch will de-bounce the Reed.
As for the LM393 in the case of the Reed Switch it will act as a buffer only.

The LM393 is a Low-Power, Low-Offset Voltage, Dual Comparator, the circuit only uses one of these comparators.
Below quote is taken from the Texas instruments data Sheet at the following link http://www.ti.com/lit/ds/symlink/lm393-n.pdf
A basic comparator circuit is used for converting analog signals to a digital output. The output is HIGH when the voltage on the non-inverting (+IN) input is greater than the inverting (-IN) input. The output is LOW when the voltage on the non-inverting (+IN) input is less than the inverting (-IN) input. The inverting input (-IN) is also commonly referred to as the "reference" or "VREF" input. All pins of any unused comparators should be tied to the negative supply.

Now if the circuit was using a temperature sensor for example the 10K resistor in series with temperature sensor makes a voltage divider for the (+IN) pin of the LM393. The 10K pot would be used as a voltage divider on the (-IN) pin to set the trigger voltage the the LED will turn on, in the case of a temperature sensor when the temperature reaches a certain level the LED will turn on.
I hope that answers your question even though it is a bit long winded, ( I like to tell stories :) )

READ-SWITCH-MODULE-SCHEMATIC.jpeg
REED SWITCH MODULE SCHEMATIC

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 Jun 01, 2018 8:20 pm

So to some up for the uninitiated, this add-on board is needed to condition signals that are floating/analogue, like the temperature sensor, which could be used on an analogue input and could have the trigger point set in software (and could therefore be changed in the program to several set points). But if an analogue input is not available or the set point will never change, it can be used on a digital input and set externally by the add-on board pot.
A simple switch or or reed switch should be all that is needed to trigger a digital input, but because the Arduino 'reads' it's inputs very quickly, any chatter (poor contact) of the switches switch point will cause several triggers (multiple spikes) to be seen by the Arduino before final switch over. The other issue is 'floating' voltage, obviously the Arduino will read 0V as zero and 5V as one, but if the input is not read in comparison to ground (floating), the arduino will struggle. The point at which the input decides to change, in theory should be 2.5V, but in reality is more like 4V and zero is 1V so something in between will confuse it and a floating voltage (wire not connected to earth or Vcc) may cause erroneous decisions. Thus its best to use a pull up resistor to ensure that when an input is not switched, it is seen correctly. A capacitor can be used to help de-bounce or a bit of code can be used to eliminate short pulses.
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

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 » Wed Jun 06, 2018 10:00 am

Well "TimberSurf" I thank you for you translation of what I had written. Unfortunately simple explanations are not one of my good points. After doing this stuff for over 50 years it is instinct and you just do it, having to stop and try to explain things simply can be difficult.

The main reason for using these complete boards is there easy to use just hook up power, ground and signal. You don't have to solder anything just use the jumper cable to hook things up and they should work. You don't need to know to much about what is going on in the beginning you just treat it as a black box you connect the wires to. A bit like the Arduino I am not going to explain the ins and outs of how it works, you just need to think of it as a black box. Just like any other piece of equipment in the house , TV Stereo, Washing Machine, ETC. You Just expect them to do what you want it by the input control you have, there is no need to understand the why's and why-fore's of what goes on inside,
If you would like more in depth explanations please ask I shall with the help of my translator try to explain everything you wish to know.

I am still putting together the next part so please be patient with me.

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

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Fri Jun 08, 2018 9:40 am

Hi all

I need some help from someone who understands how to write Arduino Sketches because I don't have a clue :roll:

So I have both UNO and MEGA boards which I use with a number of freely available sketches for things like DCC control DCC++ and for stepper motor control of a Turntable and now I wish to use one to control servos for point control

I have one of these sensor shields which I understand can control 16 servos https://www.ebay.co.uk/itm/2009-Nano-v3 ... 3004659436 if that is correct could someone write the sketch for me.

All I will need is servos connected to the shield and each servo switched by a toggle switch with end point adjustment via a simple edit of the sketch , say zero to 180 Deg for the end to end throw which I can adjust in a text editor if I need less than full throw.

If what I want is doable without too much work or time could someone help

I am ok with connections and following instructions but don't have a clue when it comes to writing code, and with the signs of dementia starting I will not be able to learn, so anyone up for the challenge and be willing to help me.

Thanks in advance if anyone can help.

Paul

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 Jun 08, 2018 1:26 pm

Paul-H wrote:I need some help from someone who understands how to write Arduino Sketches because I don't have a clue :roll:

Hi Paul H
Using the UNO you only get 6 hardware PWM ports so you van only use 6 servos unless you use software PWM pins 3, 5, 6, 9, 10, 11,
The Mega has 15 PWM pins 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46, so on that shield you will get 12 of the 15 pins, these pins are on the double row header 44, 45, 46, so are not available on the shield.

I will have a look at it for you tomorrow as it is my bed time here now. It is no problem for me to do this for you.
any other questions or needs you can just PM me and I'll give you an email address to contact me

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

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Fri Jun 08, 2018 4:58 pm

Thanks timbologist you are a star

Shall we stick with doing this for the MEGA as it sounds like its the better option of the two.

Paul

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 » Sat Jun 09, 2018 7:15 am

Hi Paul

What I will do first is write a program that you connect 1 servo at time to one of the ports, and connect a variable resister or pot to one of the analog ports.
Using the serial terminal in the Arduino IDE we can get the values for the end positions of the servo's. You will have to go through each servo and number each one and record the numbers for each servo and the position of the points whether on the curve or the straight.
And let me know how many servo's and switches you are going to use and if you intend to have leds on a mimic panel as I can incorporate all this in at the time of writing the code and I can supply a wiring diagram of what I have put where.
If you do not have a pot about 10k is ok. I can make it to use the terminal to get input to move the servo.

And for the rest of you I shall describe what I have done in a project so you can all follow and use this information.

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 » Sat Jun 09, 2018 11:06 am

Hi all
Well a bit of a side track here, this is the program I have written for Paul-H to get the values for his servo's so that we know how to move the servos maximum and minimum throw values. The program is based on an example sketch that comes with the Arduino IDE on controlling servo's, been modified for this use. I have added a piece of code to send the max and min values to the serial terminal, the values only change when a new max or min value is attained.

Part of terminal output.

MIN VALUE 58 MAX VALUE 132
MIN VALUE 57 MAX VALUE 132
MIN VALUE 56 MAX VALUE 132
MIN VALUE 55 MAX VALUE 132
MIN VALUE 54 MAX VALUE 132

The program is set up in the normal way I program, that is broken up into separate files for different parts. Just as long as all files are in the same directory it will load and compile correctly. attached is a zip file containing the full program just unzip keeping the same structure and all will be good.
The code should be commented enough for you to follow if there any problems please ask.
This was set up for an Arduino Mega but should work with others just set the board at compile time, it uses pins that are the same on all of the boards.

Code: Select all

// PROGRAM SET ARDUINO-SERVO-CONFIG-01
// FILE NAME ARDUINO-SERVO-CONFIG-01.ino
// PROGRAM TO GET INFORMATION ABOUT
// POSITIONING OF SERVOS CONNECTED TO
// AN ARDUINO MEGA
// INFORMATION FILE
// A.R.TURNER 2018-06-09
//


// THIS PROGRAM IS BASED ON THE ORIGINAL
// EXAMPLE PROVIDED IN THE ARDUINO IDE
// MODIFIED TO OUTPUT THE INFORMATION
// TO THE SERIAL TERMINAL

/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/


Code: Select all

// PROGRAM SET ARDUINO-SERVO-CONFIG-01
// FILE NAME ARDUINO-SERVO-CONFIG-01.ino
// PROGRAM TO GET INFORMATION ABOUT
// POSITIONING OF SERVOS CONNECTED TO
// AN ARDUINO MEGA
// CONFIGURATION FILE
// A.R.TURNER 2018-06-09
//

#include <Servo.h>

Servo myServo;               // CREATE AN INSTANCE OF THE SERVO
int potPin = 0;              // ANALOG PIN 0 IS USED TO CONNECT THE POT TO
int potReadValue = 512;      // VARIABLE TO PLACE THE VALUE OF THE POT READ INTO
int servoSetAngle;           // VARIABLE TO PLACE THE VALUE OF THE ANGLE TO SET THE SERVO
int lastMin = 90;            // VARIABLE FOR MIN THROW VALUE INITIALIZED AT CENTRE
int lastMax = 90;            // VARIABLE FOR MAX THROW VALUE INITIALIZED AT CENTRE
bool newValue = false;       // FLAG TO SAY IF A NEW POSITION HAS BEEN SET



Code: Select all

// PROGRAM SET ARDUINO-SERVO-CONFIG-01
// FILE NAME ARDUINO-SERVO-CONFIG-01.ino
// PROGRAM TO GET INFORMATION ABOUT
// POSITIONING OF SERVOS CONNECTED TO
// AN ARDUINO MEGA
// MAIN PROGRAM FILE
// A.R.TURNER 2018-06-09
//

void setup()
{
  myServo.attach(9);  // ATTACHES THE SERVO TO PIN 9 A PWM OUTPUT
  Serial.begin(9600); // ENABLE SERIAL PORT AT 9600 BAUD
}

void loop()
{
  potReadValue = analogRead(potPin);                    // READ THE VALUE OF THE WHICH IS A 12 BIT VALUE (value between 0 and 1023)
  servoSetAngle = map(potReadValue, 0, 1023, 0, 180);   // SCALE IT SO THAT IT IS WITHIN IN THE RANGE OF 0 TO 180 DEGREES)
  myServo.write(servoSetAngle);                         // SETS THE THE SERVO POSITION ACCORDING TO THE VALUE
  delay(20);                                            // WAIT FOR AWHILE FOR THE SERVO TO MOVE

  if (  servoSetAngle >  lastMax)                       // CHECK IF THE SERVO ANGLE HAS INCREASED FROM LAST POSITION
    {
      lastMax =  servoSetAngle;                         // SAVE NEW MAX SERVO ANGLE VALUE
      newValue = true;                                  // SET FLAG THAT WE HAVE A NEW VALID VALUE
    }
     
   if (  servoSetAngle <  lastMin)                       // CHECK IF THE SERVO ANGLE HAS DECREASED FROM LAST POSITION
    {
      lastMin =  servoSetAngle;                          // SAVE NEW MIN SERVO ANGLE VALUE
      newValue = true;                                   // SET FLAG THAT WE HAVE A NEW VALID VALUE
    }

  if ( newValue )                                        // ONLY DISPLAY POSITIONS IF SERVO HAS EXTENDED MOVEMENT
    {
      Serial.print("MIN VALUE  ");                       // PRINT TEXT
      Serial.print(lastMin);                             // PRINT VALUE OF MIN ANGLE
      Serial.print("  ");
      Serial.print("MAX VALUE  ");                       // PRINT TEXT     
      Serial.println(lastMax);                           // PRINT VALUE OF MAX ANGLE AND SET NEW LINE FOR NEXT OUTPUT
      newValue= false;                                   // CLEAR FLAG THAT WE HAVE A NEW VALID VALUE HAS BEEN PRINTED
    }
}
Attachments
ARDUINO-SERVO-CONFIG-01.zip
(2.07 KiB) Downloaded 8 times

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

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Sat Jun 09, 2018 12:46 pm

Thanks for doing this

Will have a look over the weekend and get back once I have had a play.

Thanks again

Paul


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 2 guests