Using Bluetooth and an 74HC595 to control Märklin Switch Points

Fun

My parts have arrived and it was now time to use the prefabricated relay banks with a shift register. This will reduce the cabling mess and will allow me to control all my Switches, Signals and Interrupter Tracks (see the switch post) with one or two of the banks. I also will connect the SwitchPoints to Bluetooth.

Make the switch!

There are many posts that show how to use a shift register with an Arduinio or an ESP32, I would recommend this one as it explains the whole process from the beginning to the end. For the code, I started by reusing the Bluetooth code from an earlier post and expanded it, along with the Mobile App so that switches are now also controlled via Bluetooth.

The Arduinio Code is here and the MIT App Inventor code is here.

The beauty of this is that the Arduinio can controll mutile 74HC595 with only 3 cables, reducing the amount of PINs that are required on the Arduino. Each 74HC595 has 4 pins that are used, the 3 that are connected to the Arduinio and one that connects it to the next 74HC595 in line. I don’t want to go into too much detail on the 74HC595 as there are a lot of blogs already about this. The more important part I want to focus on is how the multiple relay banks are addressed in the code. In the code (line 10) one can define the amount of 74HC595’s connected to the Arduinio as this will create an array of booleans with the correct size. The function writeRegisters() will write the array to the 74HC595’s and the 74HC595 will then operate the relay banks. The array will address the relay banks in the following way: the Index 0 is the first relay (in 1) on the first relay. The first relay is the one that is connected to the 74HC595 with the data pin connected to the arduinio (orange cable in the drawing on the right side). The first relay indexes are from 0 to 7. The index 8 would be the first relay (in 1) on the second relay, that’s the one connected to the first relay by the extension pin (green cable in the drawing on the right side). The second bank would have an index of 8 to 15. A third relay bank would then be connected to the second 74HC595 by another extension cable and would have the index 16 to 23.

There are two things that I found out in the previous posts that need to be implemented: the floodgate and the Elegoo Relay issue. In the code I declared the variable floodgate to define which relay in the bank(s) is the floodgate. Please be aware that my current code converts the string coming from the MIT App to find the index of the Switch Point to be operated. Meaning, that when one clicks on Switch Point 1 in the App, the App sends the string “sw1\n”, which then gets translated to relay 1 to be switched. I will improve that program in one of the next posts but for now, it’s usable this way.

The other issue was the Elegoo relay banks use the HIGH and LOW settings the other way around than a normal switch would do, meaning that a HIGH will put the relay in its default setting. This is important to notice if someone would use a different brand of relay banks. If that’s the case I would suggest changing any HIGH to LOW (and vice versa) in the code.

 

Making it “cleaner”

The challenge with two or more relay banks and the 74HC595 is that it starts becoming really messy fast with the number of cables needed to be used and connected. Therefore I made it a bit easier. An additional problem popped up when I assembled all the parts and found out that decoupling the 74HC595 is essential, or else you get some glutter and involuntary switching of tracks. It’s recommended to insert an 0.1 uF Capacitor (Marking 104) between VCC and GND as close to the shift register as possible. As well as connecting the External port back to GND

Below is the building plan for a small module that can be stuck onto the Elegoo 8 relay banks and requires only minimal cabling. Putting the plan into action I was faced with the issue that the connection of the module to the Elegoo needs to be on the other side of the PBC, where there is no soldering point. I came up with the solution of using a fine wire loop and soldering it in on the correct side and then wedging the connector into the whole and soldering the connector on the other side with big blobs of solder. This makes it pull-resistant, not much but enough. Not a brilliant solution, but it works. This little module reduces the number of cables required by a lot.

EDIT (02/02/23)

After building up the whole setup, I discovered problems with the original module. After a while, the 74HC595 just did stuff by itself randomly. There was noice in the system. It took me a time to figure out where it comes from…it was the Latch and the 5V input. So I needed to alter the cirtucite. The below picture are the fixed ones….yes and it became messy… but now it’s working absolutely fine. The following is required:

  • put a 100nF (104) Capacitor between VCC and GND.

  • put an 100 Ohm between the Latch (RCLK) input and the 74HC595’s Pin 12, also put a 100nF (104) Capacitor between Pin 12 and GND.

Previous
Previous

VIDM upgrade to 3.3.7

Next
Next

Controlling tracks