It's currently powered from an Amazon fire stick adapter. The code is shown above but I've forgotten how to copy to forum so that it can be scrolled separately.
Have you checked if your Hall Effect is open collector?
If so, do you have a pullup resistor?
Thanks Tom, I've tried this, though not not convinced it would work. I inserted Pullup in the Hall sensor pin mode code. Turntable going merrily around at the correct speed and direction but taking no notice of the Hall sensor which is lighting up as the magnet passes but turntable continues to revolve. From my readings on the old multimeter, the Hall is outputting high when not active and low when triggered, so I can't see a pull up helping. I ordered yet another arduino last night, suspecting I might have spiked all the inputs a while back when a different Hall sensor made accidental contact with the track. I thought it was only the Hall sensor damaged but strikes me that it's all the inputs dead, whilst still running sketch outputs. Does that sound likely?
Turntable back in operation! 2 days after ordering yet another arduino, the spare arduino (missing for weeks) turned up in a dark corner. 5V lead was replaced as had become loose. A joint in button 1 lead was also replaced, as when checked came apart. Turntable is now obeying buttonpresses and Hall sensor! No Pullup required in Hall sensor pinmode.
But alas, again I'm bamboozled. When I plug in my track voltage power supply, the turntable just turns relentlessly once again. The 4 button pins are pulled high and nothing should happen unless button 1 or button 2 is pressed. If a spare single pushbutton is grounded and attached to Keypin 2 however, turntable behaves as it should.My conclusion is that current is somehow leaking to earth via track power. I wouldn't have thought this possible but the track power is a square wave form of AC , so I assume it could be induction. I've decided to order some ethernet cable (twisted pairs) and reroute it, keeping well clear of track power cables.
In the meantime I've meticulously re-routed the existing cable all well clear of track power lines and the problem persists. Strange thing is that this problem didn't arise until I tidied the wiring and replaced both Hall sensor and arduino, all at the far end of the wiring. The symptoms suggested that button 2 was being pulled to ground by induction. It's not the button unit because it has been working fine, so long as there is no track voltage. I tried changing "delayMicroseconds(12000)" to "delay (12000)" but now nothing happens unless I press reset on arduino. ?????
Help please! Problem still not resolved. I've re-routed button cable, then removed it from all fittings, well clear of track voltage. It works as designed if track voltage switched off but goes rogue as soon as track power is switched on. There's no actual contact between the two circuits, although the Arduino itself is within a few inches of the track. Motor stops running if button 2 wire is pulled from arduino and when button wires were running through the furniture as re-routed, there was a an 0.2v drop in the logic voltage measured between button wire and ground return. Pins are set high with internal pullup.
Can you shield the Arduino itself? Perhaps hide inside a Faraday cage inside some scenery object. Or get it further from the track of course.
Thanks wildbill that's my current thinking, not in the scenery but in a box on its own shelf. Having slept on it, it's logically the next thing to try. It ran for about a month though with only occasional interference, which is puzzling.
Input pull-up resistance could be as high as 50kΩ so add external pull-up resistors to the four button inputs. I suggest a resistance value to give a few milliamps when pulled low by each button, so say roughly 1kΩ. Especially as you report the motor stops running when you disconnect button 2, I would recommend trying external pull-up resistors before messing about with the location of the Arduino.
What hall effect sensor are you using? I guess the sensor will be fairly close to the track.
Thanks Archibald. I've already embarked on the job of relocating the arduino into its own box. I'll see how that goes. It's tidier that way and easier to work on. It'll mean adding a foot or so to the hall sensor wiring but that should be OK. I'm using the internal pull ups but it's another avenue to explore if the relocation doesn't work. The Hall sensor is a KY-003 , if no magnetic field is present, the signal line of the sensor is HIGH. It seems to be working as required. Yes necessarily it has to be close to the track because the magnets are just below the turntable deck but conveniently it has an onboard LED which lights when the output goes low.
Since it was working previously, the typical developer question is "What changed last?". Is there anything obvious?
Also, how about some shielding for the Hall and button wires.
That's what's so very odd wildbill. Before there was some random interference but nothing connected with the track power. It was disassembled at the Arduino end to tidy the wiring and after putting it back together the track power started in interfere drastically. The Hall sensor is designed to respond to a magnetic field and in any case, with just the number 1 button attached it works! It does precisely what the first part of the loop stipulates. Of course that isn't very useful because the turntable can only move to the home position where it stops. If button two (when connected) is dropping low because of interference, this seems to be the only way to explain the behaviour.
That uses the 3144EUA-S hall effect integrated circuit which has open-collector output (datasheet). The associated LED and 680Ω series resistor provide a pull-up but the LED probably does not conduct until the voltage across it reaches 1.6V. This means the pull-up is essentially towards 3.4V (with 5V supply). To guarantee detection as logic high, the input voltage to the ATmega328P processor should be higher than 3.5V. This pull-up is probably sufficient in practice but an additional pull-up resistor, such as 1kΩ, could be beneficial.
But when the logic is high, Archibald, it is because the neo magnet hasn't been detected which brings the logic low. The stepper moves only when the logic is high, it should stop when the logic goes low. That's how the sketch is designed or have I got this wrong? My thinking is that the button wires are going low. If I can read a voltage drop of 0.2 V at the other end of the button wire, as a repeatable result of plugging in the track voltage, I'm thinking this may be indicating a bigger drop since the track power is oscillating at about 100 mHz , so the button wires could be dropping much lower at a high frequency which amounts to 0.2V on average. I should have it all soldered back together and in a box before tomorrow. That's tonight's plan anyway.
I agree that when the turntable reaches its home position and the magnet causes the open-collector transistor to switch on then you should get a good 'low' voltage level. I have said that the LED and resistor probably provide sufficient pull-up. Nevertheless that pull-up is barely sufficient to guarantee a 'high' voltage level. Even if this marginal pull-up is not the cause of your troubles, for the sake of the price of a resistor, I still think it would be advisable to add a pull up resistor especially as the sensor is near the track and may be some distance from the Arduino.
it is not clear to me what you are measuring when you measure 0.2V. With a button pressed you should be reading zero volts (at least less than a millivolt!).
If moving the Arduino further away from the track and adding pull-up resistors does not solve the problem, then I think we should start considering adding some capacitors to reduce susceptibility to high frequency interference. Such interference is more likely to be due to capacitive coupling from the track than electromagnetic.
I guess your power supply unit is some distance from your Arduino. Have you added decoupling capacitors to the 5V supply near your Arduino?
Archibald, the 0.2v reading is a drop in potential between button wire and the common ground wire at the button position end but with the keypad disconnected. This drop is occurring when the track power is switched on. The arduino is now in its own box several inches away from any track but the problem persists I've even snipped one of the track feed wires to the section running up to the turntable, to no avail. Arduino supply is now no more than a meter away from its power supply. I've checked this again and still getting a voltage drop of 0.15.
Wildbill, one thing that occurs to me is asking myself the question of whether I had the 5v attached to the common pin wire . I remember having to decide which was correct whilst tidying up the wiring and everything I read suggested it had to be the ground not the 5V. With the button pins set to input_pullup, what would happen if I switched the common from grd to 5v? Yet another idea I've had, can I tighten up the code by specifically prohibiting the stepper to move if no button presses are detected or maybe inserting a strategic delay? I've tested the buttons with a simple button sketch reporting to serial monitor and everything looks right, even when the track power is on.
Would you not expect the voltage, with respect to Arduino's ground, on each of your button wires to be close to 5V due to the INPUT_PULLUP resistors (with button not pressed or not connected)? The input pullup resistance will affect the reading on your voltmeter so you could expect the reading to be a little less than 5V, but not as low as 0.2V.
Are you measuring 0.2V or 0.15V on all button input wires (with respect to ground) ?
Have you connected the processor's ground to the track power in any way?
Yes it is Archibald. My multimeter reads a bit high but I get 4.39v at the end of the button wire to button common (Arduino ground) then 4.32v at the same place but with track power on. That's a difference (drop)of 0.07v but it was 0.15v yesterday and 0.2 V when the button cable was within the structure containing track power. No there's no common ground connection between the 2 circuits, in fact the track being 15v (square wave) AC, there is no ground.
Ah now here's an interesting discovery. Simple button sketch with serial print loaded again. There's no reference to a stepper in this sketch). Works correctly until track power switched on then stepper oscillates noisily and random button presses appear in serial monitor.
So, interference, as you have no doubt surmised. I would not expect that this is a new problem for model rail enthusiasts.
What have other folks done about defending electronics from track power? I think people are even using Arduinos to operate servos to switch points, so they would be all over the layout.
My strong suspicion is that your wiring is wrong.
The critical principle working with these devices which operate at radio frequencies, is to keep all connections between one part and another tightly bundled either by using ribbon-type wire, or tying with cord (nylon fishing line is convenient) so that there are no open loops in the wiring between a power or signal line and its matching ground return as such open loops function as little coupling transformers. If you can trace an open circular path around any part of your wiring loom, you have almost certainly done something wrong.