Model railroad swithces with keypad

I'm into making a simplified switching control of my Model Railway with 40 switch points.

I want to control switch points by using an Arduino Nano, Keypad 4x4 and a 16x2 display.
Everything should be connected to the I2C bus devices, to simplify wiring.

A number (5pcs) PFC8575 will be using ULN2803 to turn on gears with a short voltage pulse on command sent from the Keypad.

For example. Switch 5+* for straight track and 5+# for side track.
The display asks which switch point to set.

Pressing the keypad number + * or # for the desired operation.
The display confirms what gear to put on.

Ideally, you should be able to "toggle" between the * and # on the selected number before entering the new switch point number.

I get the communication to work well between keypad and display, but have trouble sending it on to the right PFC8575. Each unit has its own address type 0x39, 0x38 etc. The display has the address 0X3F.

How do I write the code for sending the data to PCF8575 to set the right output?

Should it be possible to store all commandos in a EEPROM and read from that to send to the PFC8575?

Is there anyone who has used PFC8575 to expand the I / O on anything resembling set that can give me some advice and help?

Can you post a circuit diagram of your project please?
How much distance have you between your controller and the 8575's?

Have you looked here and their examples?

Tom.. :slight_smile:

I have been planning an Arduino control system for a club model railway layout. My plan is to use servos to operate points (turnouts) signals and uncouplers. There will also be colour light signals. I reckon on having a standard program for an Atmega 328 (perhaps on a Nano or could be an Uno, or just on a breadboard) that can control up to 12 servos or a combination of servos and LEDs for signals. If more I/O is needed it will only be necessary to add another 328. The control (via a Serial connection) will come from a Mega which will be connected to the various operator switches. All of the "intelligence" for the system (including point and signal interlocking) will be in the Mega.

The "client" has no wish to use a keypad for selecting points and signals but it would not be difficult to do that. And I would not choose to use a keypad myself - preferring a control panel that mimics the track layout. But I would build my "control panel" on the screen of a laptop.


Here is the Circuit Diagram.
It's only a small design to show howe things are connected.
Longest distance between Controller and last PFC8575 is about 5meters.
I have 4K7ohm pullup resistors connected to the Bus.

I can't use servo for my points as I have Fleischmann Profi rails.

The problem is that I don't know how to make the code, for writing to the different units.
Keypad to Display works OK.

Forgotten the Layout. Here it comes!

Circuit Diagram.pdf (46.3 KB)

Fine on the diagram, it is better to export it as jpg and attach and link it into your post.

Did you look at the link I posted?
It has sample code.

Tom... :slight_smile:

Hi Tom,
Thank you!

I have made a test and it work :slight_smile:

But I have got another problem.
If I set P0 to write 1 the status of the output will only have a high frequency not steady 1.
If I set P0 to write 0 the status is steady 0.
Sometimes it will set the output to 1 but very randomly.

I have a ULN2803 connected to the output from PFC8575.

Tested different units but same problem with all of them.

Can you post your test code please?

Using code tags to format it into a readable window.
</> symbol

Thanks. Tom… :slight_smile:


Here is the code I’m using for test.

//   Write to a PCF8575 to control the 16 outputs using the I2C bus. The PCF8575 can sink approx. 25ma
//   but only source approx. 5 ma, so it is best to take your LED's or other devices to ground through
//   the chip instead of feeding them through it. So in my code example the 1's mean that the LED
//   is off, and the 0's mean that the LED is lit.
//   12/30/2010 Sonny Schade  Use and have fun. If you make any changes that improve
//                                               this code,Please post it to the community.

#include <Wire.h>

byte address = 0x27;   // address of PCF8575 with A0-A2 connected to GND "B01000000"
void setup()
  Wire.begin();       // join i2c bus

//control the pins

void loop(){
 byte a = 0B00000001; //controls pins 0 - 7
 byte b = 0B00000000; //controls pins 10-17
 // by changing the 1's and 0's above you can control which pins are on and off.
 // Remember the chip can sink more than it can source So the 1's are off and the 0's are on.
     Wire.beginTransmission(address);   // send the address and the write cmnd
     Wire.write(a);                      // pack the first byte
     Wire.write(b);                      // pack the second byte
     Wire.endTransmission();            // send the data



The 8575 (TI and NXP) has 16 digital I/O bits at addresses 0x20-0x27.

I think you have to setup which I/O bits are input and output.

Tom.... :slight_smile:

No, just the code I have sent here.
My devices are hard coded with addresses ranging 0X20, 0X21, 0X22, 0X23, 0X24, 0X25, 0X26 AND 0X27.
All of them has the same behavior.

What code should be sent to be able to write to the units?
Why should it only be possible to write outputs to half of them?
0x38 for write
0x39 for read
I can't change address without soldering again.

Do you have a simple code that write to the ports? Preferred address 0X24.

I have the data file for PCF8575.
This is the Address Table.

Tom… :slight_smile:

pcf8575c.pdf (1.04 MB)

In your code;

byte address = 0x27;   // address of PCF8575 with A0-A2 connected to GND "B01000000"

B01000000 = 0x40

B 0100 0000
4 0

not hex 27

Tom.... :slight_smile:

That's a comment in that program, it's not mine. Just from the net.

How should the write string look like to set P0 1 HIGH on Chip with address 0X24?
Do you have some sample?

Hi Tom,

Your Address table doesn't comply with my units. The I2C scanner program tells me the address is 0X27 where you table says 0X4E. However I have tried also with that address but no action.