Pelsea

I've been posting a lot of analog circuits lately, and while these are primarily chosen because of the concepts involved rather than the actual result, there is always an underlying question-- "would this be easier as an Arduino project?"

Of course the answer depends on what you find easy. Some folks are comfortable writing code, others prefer to stuff circuit boards. They are both simple enough to learn (as far as model railroad type projects go), but many who have already invested a lifetime in a particular skill set aren't interested in pioneering just now-- after all, everyone here has a layout to build.

The answer also depends on the project. In some cases, such as as a stepper motor controller, external driver chips are the best choice for either method and an Arduino is just a fancy switch. If you don't need fancy, the dedicated chip approach is quicker. In other cases, particularly things which require precise timing, Arduino will win hands down.

I'm going to use this blog to post occasional Arduino solutions to problems I've addressed in my Control the World with Simple Logic Circuits blog. Since the tricky part of Arduino is interfacing to the world, I strongly recommend you read that blog. I will show needed interface elements, but I'm not going to cover the background again.

At the moment, the only thing in the queue is a servo driven turntable (it completes a set), but I'll be happy to address suggested projects if you can find anything that is not already in Geoff's excellent library.

pqe

Reply 0
Geoff Bunza geoffb

Great Idea!

Hi Peter,

This is a great idea. Based on some of the questions I get, please consider controlling a 5V relay, a simple motor control -- on/off, bidirectional DC motor control w/PCM, a keypad for input, and a potentiometer for input.

I look forward to your posts.

Have fun! 
Best regards,
Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Pelsea

Arduino controlled servo driven turntable.

T-01b(1).jpg 

Those who have read my Control the World thread will recognize this as my turntable test jig, built up from K'nex parts. (These are great for prototyping, but I suggest something more substantial for actual production. The plastic is soft, and the teeth are not all that well fitted. On the other hand, $1.76 for a 5 inch sprocket gear isn't bad.) I've powered it using various DC motors, stepper motors (with excellent results) and an analog driven servo. Here, I'm using half of a joystick (potentiometer with a spring return to center) to control the direction and speed of the big gear. That last circuit wound up on the complex side, so I decided to test it against the Arduino approach.

Well, the actual circuit design goes to Arduino hands down. Compare:

SLC-17b.jpg   AT-01a.jpg 

The analog circuit is (to be charitable) a bit fiddly. As for arduino, no actual circuit is required. The Uno can just run a servo (white wire) and joystick (grey, white black) directly.

Of course the analog circuit is complete, but the Arduino requires code. I'm leaving to others the chore of explaining how to get code into the Arduino (see the December 2016 MRH), as well as coverage of the various flavors of board. All I'm going to publish is code.

And here it is:

#include < Servo.h>

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

int potpin = 0;  // analog pin used to connect the potentiometer
int inVal;    // variable to read the value from the analog pin
int outVal;   // variable to write control to servo

void setup() {
  myservo.attach(9);  // attaches the servo object to pin 9
  myservo.writeMicroseconds(1500); // no movement
}

void loop() {
  inVal = analogRead(potpin);// reads the potentiometer (0-1023)

  if((inVal> 508) && (inVal < 516)){   // create a null zone 
     myservo.writeMicroseconds(1500);  // no motion if stick is centered
  }else{  // move that puppy
     outVal = map(inVal, 0, 1023, 1300, 1700); // scale inVal to get microseconds
     myservo.writeMicroseconds(outVal);          // sets the servo position
  }
  delay(15);  // updates every 15 ms-- probably fast enough
}

It's a bit embarrassing actually. The black belt coding required to get precise timing for servos is hidden away in the Servo.h library, and there is a servo example that does almost everything needed. My changes are fairly small-- I'm specifying the servo control in microseconds rather than the (somewhat unreliable) degree conversion, and I've created a null zone in the center of the joystick.

The null zone is there because the joystick does not return to the exact center of its range when I let go. (That was a real headache with the analog circuit.) If I use the map function to convert inVal to outVal, I will only get the standstill 1500 value if the read gives me 512 exactly. 511 or 513 will cause the motor to creep ever so slowly. In fact, the Arduino will read 511 as often as 512 even when the joystick does not move at all. In this version, any value from 509 to 515 will be treated as the center. That leaves a couple of really slow motions out, but they aren't missed. This is the key line:

if((inVal > 508) && (inVal < 516)){

The && operator is AND, which will be true if what precedes it and what follows it is true. (The compiler is smart enough to solve the outside clauses first.) So if inVal is both above 508 and below 516 the first part of the if statement will happen (and the else part won't).

Here's a movie to prove it works:

That's it for now.

pqe

Reply 0
Pelsea

Y.D.I.Y?

I recently saw a post on one of the other Arduino threads that posed the question "Why take the time to learn Arduino and electronics when there are plenty of commercial products, both kit and ready to run that do more or less the same things?" I've ruminated about this before, and agree with almost all of the reasons others mention about costs and intellectual challenges and keeping an active brain. But it occurred to me that the primary reason for me is elegance.

Elegance in electronics is hard to define. (It's one of those things engineers recognize but seldom talk about.) It's a circuit that uses as few components as possible but no fewer. It's solving a problem for 75¢ that anyone could do for $10.00. It's software that just works.

When I build my own circuit, I am taking responsibility for all of that. What's more, I am building for my own well understood needs, not meeting a price point or a ship date (the twin scourges of tech companies). Think of it as super-detailing. I breadboard multiple versions of a circuit, scour the web for other takes on the problem, even read the data sheets all the way through. When I build, I'm constantly trying to find the clearest layout of parts, the fewest crossed wires-- heck, I even take pains to see that the colors on resistors are readable from left to right or top to bottom.

When I write code, I follow the advice I always gave students about comments and meaningful variable names, use optional parenthesis to clarify complex if clauses, and avoid x = ++x> 11 ? x - 12 : x. I also test my code with illegal inputs, run it a long time, and find folks who know nothing about it to try it for me. Every time I make a change, I run all of the test cases again.

I aspire to be the same kind of artist as someone who uses both acrylics and oils as well as powder to weather a boxcar, who considers coupler levers a basic detail, who will spend three days on underbody gear. I have a box of circuit boards that will never see the light of day because they didn't come up to snuff, each one representing a week's work. The servo circuit above is version 6-- the first 5 were analog. One was good enough for publication on the other thread, the others were OK, but lacked something.  Boxcars get stripped and repainted, protoboards get salvaged.

So I'm going to stop saying electronics and coding are easy-- they aren't. They require mastery of some tricky intellectual concepts and a lot of fussy work. Sure, you can follow cookbooks (and that is the best way to start out) and get OK results, but eventually you will need to branch out and build something that is uniquely your own. When  that comes together, you will find a joy you have never known.

pqe

Reply 0
Neil Erickson NeilEr

Y.D.I.Y.

I couldn't agree more. Thank you for taking time to write what few of of would say - elegance. There are many, many expressions of this in a model railroad from laying your own track and switches to painting backdrops. Yes, there are lots of good, maybe better, solutions than what I do myself but none are my own creation. Good or bad. 

Your tutorials on simple circuits has re-kindled an old interest dating back to Morse code and building my own radios as a kid. Later hacking an IBM 360 to get free computer time on the network. I taught my wife Basic and some Pascal and enjoy wiring as much as fooling with C++. Lately my interest has lingered on battery powered engines with sound. There is so much to learn that it is a hobby in itself.

My large scale trains are begging to get back in the garden and the idea of learning Arduino with WiFi or Bluetooth controls make it all new again. Now if I could figure out how to divide voltage to limit the input to the Arduino below the threshold .... 

Neil Erickson, Hawai’i 

My Blogs

Reply 0
Pelsea

Zener diodes are your friend

Also, consider Xbee radio for communications It has a longer range than bluetooth (300' outdoors) and is not as subject to interference as wifi. There used to be a dedicated Arduino (fio) but it's now available on a  shield.

pqe

Reply 0
Neil Erickson NeilEr

Xbee

Thanks! I ordered one so now have to figure out how to address it. The Bluetooth controller has me preoccupied as it will interface with my phone and thus provide a tactile way of running the train using the BlueRails board. I thought outdoors would be a challenge so will try the Xbee as the weather improves (assuming it quits raining someday). January and February here are often the best time of year to be outdoors.

Neil Erickson, Hawai’i 

My Blogs

Reply 0
Taddeoj

Elegance and insight

a useful book is The Art of Electronics by Horowitz and Hill.  Lots of good material that is practical as well.

Reply 0
David Husman dave1905

Easy?

Quote:

So I'm going to stop saying electronics and coding are easy-- they aren't. They require mastery of some tricky intellectual concepts and a lot of fussy work. Sure, you can follow cookbooks (and that is the best way to start out) and get OK results, but eventually you will need to branch out and build something that is uniquely your own.

This is what I find frustrating.  A lot of proponents write about how I can just connect an Arduino to anything and make it work.  Yes, you can, if you know what components you need, how to get them, how to connect the various parts and you know how to write the code that controls it.  Its not exactly plug and play (what do all those seemingly random red and black jumpers do and how do you know where to connect them?).

I write my own Access apps to generate car cards and waybills.  I took a sample physical car card and waybill from a friend about 20 years ago and reverse engineered how to build an app that makes car cards and multi-position waybills.  I have gone through multiple iterations for my own layout, have built custom versions with custom waybills for other people (a Santa Fe version has 6 varieties of waybills and car orders), have added custom colors to indicate blocking, back dated a copy to make 1890's era "car tickets" and then in about 2 hours last night, rewrote that to make more traditional CC&WB  that resemble 1890's car tickets.  For me that's very similar to a lot of the electronics projects.  If you know what you are doing its really simple.  If you don't, its going to take years of practice and fiddling to get exactly what you want, especially if its something more than just a blinking light.

Dave Husman

Visit my website :  https://wnbranch.com/

Blog index:  Dave Husman Blog Index

Reply 0
Kevin Rowbotham

Not universally true...IMO

Quote:

If you don't, its going to take years of practice and fiddling to get exactly what you want, especially if its something more than just a blinking light.

Dave Husman

Dave,

I hear what you are saying, but I know people who are managing in spite of the learning curve, to do what they want with the Arduino and other micro controllers like Picaxe for example.

Is it for everyone?  Obviously not.  Can everyone figure it out as easily as the next guy?  Probably not.  Does that mean it's going to take the average guy who want's to learn, years to get anywhere, as you suggest?  I doubt it very much.  This platform was developed with youths in mind afterall...

Regards,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
George Sinos gsinos

Speaking very generally,

Speaking very generally, hardware is a couple of levels more difficult than software.  Once you get past plug-and-play kits there just seem to be more complications. I've seen programmers turn out great, well loved applications using the most inappropriate software tools for the job. But once you know a couple of software tools you start to see the commonalities, and you can almost use any tool for any job by beating it into submission.

With hardware, on the other hand, being the world's expert on math and electronics theory only goes so far. You also need to understand the physical properties of the devices, be familiar with the standard components that may be available, figure out how to assemble the stuff, keep it cool, make it fit in the allotted space... the list goes on.

that's why the software guys almost always are waiting on the hardware. Iteration just take longer.

gs

 

 

Reply 0
Pelsea

10,000 hours

Research suggests it takes 10,000 hours of serious work to master a skill. Bill Gates' career is often used as an example-- he pretty much lived in a computer lab as a teenager. I'm old enough to have logged that many hours in several fields, but I'm not sure I'm up for another go. (That's about 4 years of full time work.)

Luckily, there's no need to completely master programming/electronics to get useful results. Arduino is designed for quick learning (there's a lot going on you don't need to know about), and you only need about a half dozen circuits to connect to the world of model railroading. In fact, I bet your first 10 hours of work will yield you something you can use on your layout.

A couple of books will make it go faster. I'm not going to send you to Horowitz and Hill, that's like reading Grey's Anatomy for a first aid class. Beginners will do much better with "Make: Electronics: Learning Through Discovery" by Charles Platt. For the coding side I suggest "Programming Arduino: Getting Started with Sketches" by Simon Monk.

Of course I'm also hoping you will read my Control the World with Simple Logic thread for circuit basics and Building Simple Circuits for techniques to wrangle the hardware.

pqe

Reply 0
Taddeoj

Fair Enough

I am looking at this through EE eyes.  I do think H & H helps develop insight and elegance with respect to electronic design--but I read it after having a first course in electronic devices and so it may be too much for absolute beginners. 

I used this book a lot during my graduate school days as I was designing small circuits for use in the Physics lab.  

Bob Pease had a lot of good articles on analog circuit design, but these were also more advanced topics.

Anyways, good luck to those of you experimenting with electronics!  

Reply 0
sfupbn

CONTROL THE WORLD - Arduino style - a wishlist

Pelsea,

Welcome from DownUnder.

At the outset let me record my thanks to yourself, Dr Geoff Bunza, and  others, and especially MRH for the material that has appeared as posts or in the magazine regarding Arduino.

I am just starting out having just purchased an arduino Mega board last week.

Over the years I have used Atlas 21st Century-Customs Signals components to install a colour light signal system on a friends large 'O' scale layout and also my own 'O' scale layout.

I also designed and installed the signalling electronics, lower quadrant semaphore signals , on Arakoola see

which was transported from Australia , exhibited at the Guildex 'O' scale, Telford, England in September 2016 and then returned to Australia.  

All three layouts are DCC and  use NCE BD20's as the detection interface and Arakoola uses Tam Valley 'Singlets' as the servo driver.

Custom Signals is in the process of being sold and I am unsure of the supply situation regarding Atlas 'O' scale signals so for the future I am leaning towards Arduino driven systems.

I realise much has been published about Arduino signals systems and at present I have started reading ' 'Arduino Model Railroad Signals and Other Projects' by Paul and David Bradt which gives practical examples to construct.

However their Arduino systems all appear to be of the 'light chase' type i.e. red/yellow/green running on 1.5 sec delay.

Can I ask that you consider the following:-

Interfacing Arduino to say a 3 block 3 aspect system which either latches or hold to red when loco or lighted cars are in section..

the control of a passing siding.

regards

 

John L

 

 

Reply 0
Neil Erickson NeilEr

Wish list

If I could add to the wish list it would be a way for the arduino to act as a decoder for mu'ing freight cars to the engine as it couples in order to modify the acceleration/momentum (Simulated mass). Somehow the idea of passing a wand over the car or using an infrared light to trigger this seems unnecessary. Sound cars are an example of this. 

Is this a crazy idea or worthwhile to pursue? It seems like an ideal use of the arduino combined with some proximity sensors. Batteries on board could ensure that the signal isn't dependent on track contact e.g. Keep alive circuit. 

With the interest in realistic control of our trains using throttles, notching and even dynamic braking, this is the last "manual" side of the models to be incorporated into the equation. 

Neil Erickson, Hawai’i 

My Blogs

Reply 0
Pelsea

Interesting idea

Neil's wish is actually practical using RFID devices-- those are the little tags stores use to keep track of inventory, or that the state of California uses to track cars on toll lanes. They don't need batteries, they are powered by the signals used to read them. The tags are cheap enough that every car could have one. You could bury a reader under a scale, and generate a consist just by running the train over it. You could use an Arduino to build the reader itself and send to data over to  a JRMI machine to set the momentum in a throttle or decoder.

It's  long way from idea to implementation though, and I plan to devote this blog to smaller projects. In fact, it won't be about projects per se, but providing details about the little puzzle pieces that make up a project.

There are some gripes floating around that these tutorials are a waste because the examples are trivial. What the gripers overlook is these tutorials are not about project design, but coding and circuit techniques.

pqe

Reply 0
Kevin Rowbotham

Gripers...

Please keep doing what you are doing Peter.

Pointless gripers and trolls are what is truly trivial.

Regards,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Pelsea

Gathering data with analogRead()

AT-02a.jpg 

I always design and test my circuits on a breadboard. Since Arduino projects always involve wires from  headers to the breadboard, any movement between the two can cause trouble. So I nail my Arduino and breadboard to a scrap of wood to hold everything solid. I also do most code development on an UNO, even if the project is ultimately destined for one of the smaller boards. What you see here is the setup I used for today's lesson.

Getting Voltage Values

​All Arduino boards have a bunch of pins labeled Analog In (even though they can also be used for digital functions.) The number of analog ins and the resolution depends on the board. Things you need to know--

  • Resolution is the number of bits in a full range input value. The default is 10 bits, so the numbers range from 0 to 1023. This is not the resolution of high fidelity audio, but is fine for figuring which way a knob points. Since each bit represents 1/1023 of the range we are actually getting steps of about 5mV. The Due and Zero boards have 12 bit resolution, and any board can be set to less than 10.
  • Range is the voltage that can be applied to an analog pin without damaging anything. This is never more than 5 volts, and is 3.3 volts on some of the boards. (It matches the power requirement.) There are provisions for using different references, but that's only needed by esoteric devices. Exceeding this range either way will damage the Arduino chip.
  • Acquisition Time is how long it takes to get an accurate reading. Arduino uses a rather slow technique for analog to digital conversion, and takes about 100 µs to return a value. Your program stops to wait for the result, so reading all 8 pins at a swoop may not be the best approach in time critical applications. (But I've never had a serious problem with this.) 

The analog pins are 14-19 on the Uno, but have alternate names A0-A5. If you use the alternate names, your code will work on any board.

A voltage range larger than 0-5v can be reduced with a voltage divider. Remember the formula for the middle voltage:

Circuit1.png 

Vout = Vin * R2 / R1+ R2

The ratio of the resistors determines the voltage reduction, and the total of the resistors determines the current draw on the source.

If there is any concern that the input to an analog pin may go below 0 or exceed 5 volts, the input can be protected by a Zener diode. This is a diode with a specified breakdown voltage, and is beefy enough that it does not mind being broken down.

erSource.png 

The Zener is connected as shown above. Normally, the diode does not conduct, because it is backwards to a positive voltage from the source. If the source goes negative (below 0 v) the diode will conduct and the input will be clamped at ground (or close enough to do no damage). If the source exceeds the Zener voltage (5.1v for the 1N4733) the diode will conduct backwards and the input will be clamped to the Zener voltage. A circuit like this should be included if the voltage source is at all unpredictable, such as a piezoelectric pickup or anything with a coil.

Potentiometers

The big question on any project is "What is there to read?" The answer is any reasonably steady voltage between 0 and 5 V. There are many devices that provide this ranging from magnetic compass sensors to sonar units. The most common is the simple potentiometer, which is a resistor with a movable tap. This is what is behind most knobs on our electronic gadgets.

pots-r.png             Pots-s.png           pot-t.png      

The most common potentiometers are either rotary or slide types, but there are others that are turned with a screwdriver, or respond to flexing, pressure or nearly any kind of motion. In most cases, there are three pins, one at each end of the resistive element and one for the movable tap or wiper. A little bit of experimentation with an ohmmeter will identify them.

The drawings below show how potentiometers are hooked up. The general rule is that the load on the wiper must be 10x the resistance of the pot. Since the input impedance* of the Arduino pin is quite high, the actual value of the potentiometer is not important, as long as we don't overload the power supply. 10k is a common value that will draw half a milliamp at 5 Volts. 

chematic.png            otInside.png 

[*Input impedance or load is a measure of how much current a device requires to operate. It is specified in Ohms, and you calculate the current for a given voltage in the usual way.]

Hooking up

You can apply any sort of signal to a pot-- audio volume controls are a common example-- but for Arduino controls we stick with plain DC, with ground at one end and 5 volts on the other. The 5 volts should be taken from the 5V pin on the Arduino. That prevents any chance of an overvoltage. The wiper is connected directly to an Arduino analog pin.  The hardest part about using a pot is to wire it up so it works the right way-- usually a clockwise turn increases the voltage. This is trickier than you'd think because you often wire them from the back. Here's the setup I'll use in the following examples:

AT-02b.jpg 

The red wire is plugged into 5V, the black wire into GND, and the white wire into A0. Note that I solder my jumper wires to pin headers for a reliable connection.

Code

Let's look at some code that exercises the analog input.

The magic incantation to get an analog value from analog pin A0 is analogRead(A0). Of course you need to assign this to the variable of your choice:

int potValue = analogRead(A0);

The examples in Arduino include AnalogReadSerial which prints the value obtained from a pot to the serial monitor.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

To read the output, just open the serial monitor and set the Baud rate to 9600. You will see a steady row of numbers that are mostly the same. If you turn the pot, the numbers will change. I'm going to refine this just a bit in this code:

int oldSensorValue = -10;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  if(sensorValue != oldSensorValue)  // print only if changed
  {
  Serial.println(sensorValue);
    oldSensorValue = sensorValue;
  }
  delay(1);        // delay in between reads for stability
}

Usually we only want to take action if the value at the pot has changed, so I'm saving the previous reading in the variable oldSensorValue. When a new reading is taken, I only print it if it is different from the previous one. When I run it now, I get a stream that looks like this:

770
769
768
769
768
767
768
767
766
767
766
767
766
767
766

Cleaning Up Errors

The numbers at the top of the list change as expected because I was turning the knob. However, I let go around the middle, yet numbers kept changing. In fact if you look at the top, numbers are sometimes out of order. These are reading errors, caused by all sorts of effects including electrical interference from the Arduino chip itself. Often, this is not important. If we are just controlling the brightness of some LEDs or the speed of a DC motor, little variations will not be noticed. But sometimes it is important. Remember the turntable controller I started this thread with? These errors were causing the servo to turn when it was supposed to be standing still. So I will do what I did there-- take action only on changes bigger than one bit.

  if(abs(sensorValue - oldSensorValue)> 2)  // print only if changed by 3

The abs() function removes minus signs from numbers. Applying it to the difference between two values gives the absolute difference or distance. In this case the code wil ignore changes of less than 3 in the reading.

2
5
8
11
14
17
20
23
26
29
32

This costs some accuracy, but results in a smoother control in many situations. 

A Hardware Fix

To get the highest resolution and clean up the errors, I use a slightly more complex circuit:

otBuffer.png 

The triangle represents a chip called an operational amplifier, or opamp for short. I've discussed them a bit on t his post, but in essence they seriously amplify any difference in voltage between their two inputs. In fact, the amplification is so strong that all you see is either the V+ or ground. This makes a handy comparator, flipping positive or negative if one input is greater than the other, but we can tame the response by applying some of the output to the inverting input. Generally this is through a voltage divider, so we get some gain, but if we connect the output directly to the inverting input, the output winds up locked to the non-inverting input. This configuration is called a voltage follower. The benefit in this application is elimination of read errors that are due to electrical noise*. These errors get worse as the pot is moved from the Arduino pin, so a secondary benefit is the ability to mount the pot some distance away.  

[*The noise is reduced because of the feedback on the opamp. Any change at the output is applied to the inverting input and effectively subtracted. Opamps are way cool chips.]

There is still some jitter if the voltage at A0 is between steps, but that will never be more than one bit. This clause will take care of it:

if(abs(sensorValue - oldSensorValue)> 1)// print only if changed by 2
  { etc.

You can't use just any opamp with Arduino-- you need one that plays well with 5V power and can swing its output all the way from ground to V+ (advertised as rail-to-rail). The TLV277x series or TLV246x are a good choice, if a tad expensive. (The x is replaced with 1, 2 or 4 for the number of amps on the chip.) AdaFruit keeps TLV2462s in stock for $2.95 ea. There are more elaborate schemes that let you use cheaper op amps, but since they involve powering the opamp at ± 12 V I wouldn't bother just to save a dollar.

Usually opamps come in pairs or quads. Singles are available, but generally cost as much as a dual.  Here are the most used pin layouts:

mpSingle.png  pampDual.png     pampQuad.png 

Note that each has a distinct power supply connection.

That's about all there is to getting analog data into the Arduino. Please post your questions and corrections. Next episode will cover digital input.

pqe

Reply 0
ILHO

Current limits

Thanks for leading us along the electronic path.

I have questions: As the pots resistor changes from 0 to say 10k is there a possibility of burning up wires or Arduinos? As in hooking up a wire between the positive and negative poles of a battery makes for a lot of heat - and at 0 Ohm resistance it would seem a similar situation.  I have not found that to be true but am confused as to why not?

Is there a limit to the number of pots , 6 or 8 on small boards and 16 on Megas would be the pin limit on the boards. i have seven hooked up to control a robot arm that i pretend to use to unload logs from cars and dump them into a wood chipper to make wood pulp. They seem to work ok - I Will implement your coding to smooth things - the major problem I have is the initial jerk I get when things are powered up - more to do with that many servos draining the power supply i think - that is not a pot problem AFAIK.

OK so my questions are more of Why is this working than what is the problem, i guess.

Thanks again

Ivan Lee Herring

Mountain Valley Western

Reply 0
Neil Erickson NeilEr

Perfect timing!

Pqe: 

Thank you for this as I have been trying to understand how these parts play together. Did I mention this somewhere or do great minds ...

My application has little to do with trains but the question was brought to my attention today about what I am trying to read. A battery that is charging or discharging may give a false reading and should be measured when left idle for a while. To get meaningful data do I need to be clear about what or when the voltage reading is important to me? Under load it may appear to be quite low so maybe a time delay at or below that level would be a more accurate way to trigger events. 

BTW These "meaningless little projects" are fantastic and lead me to think beyond what is presented here (Even if a little far fetched). Thank you!

Neil Erickson, Hawai’i 

My Blogs

Reply 0
dark2star

Thank you

Hi,

thanks for your interesting post on pot's and OpAmp's - this is "tricks of the trade" stuff. Sure you get along without these but you'll spend a lot of time trying to find errors that really aren't there...

@ILHO: If I understand your question correctly, you want to know if a potentiometer connected between +5V, Ain and GND will have the same effect as a wire between the +V and GND contacts on a battery? The way you connect a pot will put the 10k resistance between +5V and GND. The 10k value is fixed and doesn't change. The wiper contact, which goes to Ain is a voltage divider (as detailed above). Finally the Ain pin on an Arduino (in Analog input mode) has a high impedance (resistance), so it senses the voltage. With everything having high resistance values, there is only a few miliamps of current - which won't burn any wires. Does this help somewhat?

Have fun!

Reply 0
Pelsea

Servo hijinks

Ivan-- I've got some drawings somewhere that should answer your questions. I'll post something after breakfast. The short answer is burning anything is very unlikely (although I did have a student's project burst into flame during final presentations once). 

As for the servos-- servos have an internal position sensing system that tries to match the voltage from a pot on the shaft to the incoming control voltage (derived from a pulse width). When the power comes on, the voltages won't match. So the servo "hunts" for the right spot. If the power comes up slowly the servo might thrash around several times before settling down.

pqe

Reply 0
Jim at BSME

Re: Servo hijinks

I understand the problem with digital servos is if the powered is applied to the them before there is a signal on the the control pin, they will do the seek you refer to, so the trick is the get the signal up then power on the servo.

This was discussed in a comments in one of Geoff's blogs ( DCC servo decoder

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
Kevin Rowbotham

Thank-you Jim!

Quote:

Kay Sievert used a pin to control a TIP120 transistor that provided power to the servos after the sketch was running to provide the position information, his resolution including a PCB can be found here: DCC servo decoder

- Jim B.
 
I've been trying to recall where I saw that!
 
Regards,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Pelsea

Ohm's law and other pertinent legislation.

Here's what my whiteboard looks like halfway through my introductory Ohm's law lecture:

Parallel.png 

(I means current, V is voltage, and R is resistance of the whole circuit shown in a single lump.) I start with the law of course, then show what happens if the current has two possible paths. Ohm's law holds true for each path, so the total current is just the sum of the currents through each resistor. Both paths have the same V, so the math is easy. I then do some algebra and derive the formula for the equivalent resistance of two Rs in parallel. This is the "reciprocal of the sum of the reciprocals" formula and turns up all over the place in electronics. If there were a third R in parallel, its reciprocal would also be added to the lower part.

It is illuminating to stop here and run a few example calculations. For instance, if V = 5, R1 = 1000, and R2 = 1000, the total current is 10 mA. That is twice what you would get with R1 alone, and is not a very surprising answer. The equivalent R is 500. In fact, if all of the resistors are the same, just divide that value by the number of resistors to get the current draw. So to answer Ivan's question, sixteen 10k pots on a 5 volt supply are going to draw the same as a single 625 Ohm resistor or 8 mA.

The next example is to make R1 1000 and R2 10,000 Ohms. That requires the messier formula. 1/1000 is 0.001 and 1/10000 is 0.0001, so the fraction simplifies to 1/ 0.0011 or 909. That works out to 5.5 mA instead of the 5 mA we would get with just R1. In the electronics trade this is a trivial difference. Why? Because our resistors are only accurate to 5% each. In fact, most components are even worse, with 10% or 20% tolerance.  Most of the time this stuff cancels out, but sometimes the slack gangs up on you. The only way to survive is to overdesign everything, so if a circuit calculates to draw 100 mA, we make sure 200 mA are available. On the flip side this means that if there is a small resistor or something hogging current, we don't sweat attaching some high value ones. (if R2 were 100k, I wouldn't even bother with it in the calculation.)

By the end of the day, the students are looking at this:

rComplex.png 

 I derive the formula for a voltage divider by using the same current through each resistor. But then I attach a third resistor to the mid point of the divider. How does that affect things? Well, you have to substitute the equivalent resistance for R2 in parallel with R3 for the original R2 using the reciprocals equation. That math gets a little messy, and the results are messy too. Suppose R1 and R2 are the legs of a potentiometer-- you no longer get a nice linear sweep of voltage, because at the top R3 overwhelms the effect of R2, but near the bottom R3 is comparatively negligible. The easy way out of the conundrum is give R3 a value big enough that we can just leave it out of the equation. 10 times the value of R1 + R2 ought to do it.

In the electronics world, R3 is probably not really a resistor, but more circuitry that draws the same current R3 would. We call this "the load". Students always have a hard time getting their head around the fact that a large load resistance is a light load (because it pulls less current than a small resistor). In audio, we speak of load impedance (or input impedance), which is like resistance but varies according to the frequency of the signal.

There are areas of electronics where they don't take the easy way out, because it wastes a lot of current to do so. This is the power company, where the amps have kilo- in front instead of milli-, or the phone company, who have to push signal down many miles of wire. They always worry about load matching, where the load is R2 in the left hand circuit, and avoid voltage dividers. R1 is called the source impedance or output impedance. In model railroading you encounter load matching when you look for a 4 ohm speaker to go with a decoder. The iron clad rule about output and input impedance is Zin > = Zout (Z is the symbol for impedance. I was already taken when they thought this one up.)

Another factor where taking the easy way out can cause trouble is radio frequency interference (RFI). When the impedance at the end of a wire is very high, it makes a dandy antenna, and can pick up a lot of random currents from sources such as the sun and KLOUD radio. Once you get to the point where the impedance is a megaohm or more, you better keep the wire very short. That's what was happening in the previous circuit when the potentiometer was connected directly to A0. You see, the input impedance of the analog pins on an Arduino is very high (100Meg). Stick a couple of inches of wire on one and the reading will jump all over the place. The opamp cured that, because although the chip also has pretty high input impedance, it is low enough to suppress RFI. The 100k resistor will bring the impedance lower if you need a long wire between the pot and opamp.

otBuffer.png 

The opamp has a very low output impedance which will also damp out RFI, so it doesn't have to snuggle up too closely to the Arduino.

class dismissed

pqe

 

 

 

 

Reply 0
Reply