How can I use an Arduino Mega 2560 to turn on and off 42 separate relays directl

I have built 6 sets of relay boards, each board has 7 6pdt relays that can be used to switch 7 aircard SIMS with an 8th SIM called the "utility" SIM. This utility SIM is off line but cannected via circuit board traces to the normally open side of all of the 6pdt relays. If for some reason an Aircard locks up and needs to be rebooted with a different SIM, my circuit can shut down the locked up SIM, throw the appropriate 6pdt relay putting the "utility" SIM into the Aircard.

To make it simple, how can I sit at my PC, communicate with the Mega 2560 and energize the correct relay(1 of 7)? I have all the electronics built including ULN 2003 drivers for the relays so all I need is a way to have the Mega 2560 put +5 volts DC on one of the Mega 2560 pins thereby enegizing the correct relay; swapping the locked up SIM with the utility SIM. Once the swap has happened, the aircard is re-energized with the Utility SIM and Verizon is advised that the locked up Aircard has been corrected. Once Verizon confirms the aircard is back on line, the original SIM must be put back into the Aircard. This is accomplished by energizing the unlatch coil of the 6pdt relay and the original SIM is back in the Aircard, Verizon confirms it it the correct SIM and all is well again. Never mind all the circuitry to be sure the SIMs are not swapped with power on or that the relay using the utility SIM can not be returned to normally open until the utility SIM is powered down, etc.

So can I get help creating an Arduino program that can energize (latch) and then via a different set of commands, de-energize(unlatch)42 separate relays from in front of my PC?

You should have asked here before you started building.
A TPIC6B595 (instead of the ULN) can drive up to 8 relays with only three wires from the Arduino.
A whole lot of them can be daisy-chained, using the same three pins of the Arduino.
An Uno or Nano can easilly do that.

Now you need 42 pins and a ratsnest of wires.

Do you know how to turn a LED+resistor connected to a pin on/off.
Exactly the same as switching the input of the ULN HIGH (on) or LOW (off).
There are examples in the IDE.

Google "IO Expander". Sparkfun makes a board, others do also.

How do you intend to control the relays? Serial port? If so, read the updated Serial Input Basics thread.

I have written the code to latch and unlatch 42 separate relays. Is there a way to latch and then unlatch each of these 42 relays as needed using my PC keyboard or mouse to talk to the Mega 2560?

Okay, the simple question is, can I use my PC mouse and keyboard to directly turn on and/or off individual relays connected to various Mega 2560 header digital I/O terminals?
I guess I would need some kind of display on my PC monitor where I could drag the mouse curser over to the appropriate relay icon and then depress the mouse to either latch or unlatch that relay.

Duplicate post

You will need application on the PC side that reads your mouse or keyboard, formats a data packet with the relay state information and communicates with the Mega via a serial port. Then the Mega would parse the packet to get and set the relay states. My choice for the PC side would be Processing as it is what I know. Information in Robin2's serial input basics might help set up the communication.

Yes, you can.

As said, read the serial input basics thread linked earlier and use example 3. Test with serial monitor.

Next develop the pc application that gives you a nice user interface that sends the data in the expected format. You can find plenty examples on the web how to control the pc's serial port and send data.

I am all but finished my long and drawn out program that lets me press a GUI button in Python/Tkinter and have it latch a relay using an Arduino Mega 2560 board.  Actually there are 14 separate commands to latch and unlatch 7 different relays.  That works fine but........what code is needed so I can take the info from the Arduino board (voltage present at a digital output pin because the relay is latched) and send that fact back to the Python program to maybe do something like change the color of the button that was pressed to get that relay to latch.  I need some kind of feedback on the GUI screen that confirms that a relay has changed state.

Below are my programs for the Python and the Arduino.  I know they are stoneage but they do work.  I just need help sending Arduino data back to the GUI to confirm an action has taken place.


char serialData;

int sim1Pin = 51;
int sim2Pin = 22;
int sim3Pin = 23;
int sim4Pin = 24;
int sim5Pin = 25;
int sim6Pin = 26;
int sim7Pin = 27;
int sim1analogPin = 1;
int sim2analogPin = 2;
int sim3analogPin = 3;
int sim4analogPin = 4;
int sim5analogPin = 5;
int sim6analogPin = 6;
int sim7analogPin = 7;
int pin31 = 31;
int pin32 = 32;
int pin33 = 33;
int pin34 = 34;
int pin35 = 35;
int pin36 = 36;
int pin37 = 37;
int pin41 = 41;
int pin42 = 42;
int pin43 = 43;
int pin44 = 44;
int pin45 = 45;
int pin46 = 46;
int pin47 = 47;

int threshold = 300;

void setup() {
  pinMode(sim1Pin, OUTPUT);
  pinMode(sim2Pin, OUTPUT);
  pinMode(sim3Pin, OUTPUT);
  pinMode(sim4Pin, OUTPUT);
  pinMode(sim5Pin, OUTPUT);
  pinMode(sim6Pin, OUTPUT);
  pinMode(sim7Pin, OUTPUT);
  pinMode(sim1analogPin, INPUT);
  pinMode(sim2analogPin, INPUT);
  pinMode(sim3analogPin, INPUT);
  pinMode(sim4analogPin, INPUT);
  pinMode(sim5analogPin, INPUT);
  pinMode(sim6analogPin, INPUT);
  pinMode(sim7analogPin, INPUT);
  pinMode(pin31, OUTPUT);
  pinMode(pin32, OUTPUT);
  pinMode(pin33, OUTPUT);
  pinMode(pin34, OUTPUT);
  pinMode(pin35, OUTPUT);
  pinMode(pin36, OUTPUT);
  pinMode(pin37, OUTPUT);
  pinMode(pin41, OUTPUT);
  pinMode(pin42, OUTPUT);
  pinMode(pin43, OUTPUT);
  pinMode(pin44, OUTPUT);
  pinMode(pin45, OUTPUT);
  pinMode(pin46, OUTPUT);
  pinMode(pin47, OUTPUT);

  // put your setup code here, to run once:


void loop() {
  int analog1Value = analogRead(sim1analogPin);
  int analog2Value = analogRead(sim2analogPin);
  int analog3Value = analogRead(sim3analogPin);
  int analog4Value = analogRead(sim4analogPin);
  int analog5Value = analogRead(sim5analogPin);
  int analog6Value = analogRead(sim6analogPin);
  int analog7Value = analogRead(sim7analogPin);
  if (Serial.available() > 0){
    serialData =;

if (serialData =='2'){
      digitalWrite(pin31, HIGH);}
      digitalWrite(pin31, LOW);
if ((analog1Value > threshold) and (serialData =='2')){
        digitalWrite(sim1Pin, HIGH);}
        digitalWrite(sim1Pin, LOW);
if (serialData =='3'){
      digitalWrite(pin41, HIGH);}
      digitalWrite(pin41, LOW);
if (serialData =='4'){
      digitalWrite(pin32, HIGH);}
      digitalWrite(pin32, LOW);
      if ((analog2Value > threshold) and (serialData =='4')){
        digitalWrite(sim2Pin, HIGH);
        digitalWrite(sim2Pin, LOW);

if (serialData =='5'){
      digitalWrite(pin42, HIGH);}
      digitalWrite(pin42, LOW);
if (serialData =='6'){
      digitalWrite(pin33, HIGH);}
      digitalWrite(pin33, LOW);

      if ((analog3Value > threshold) and (serialData =='6')){
        digitalWrite(sim3Pin, HIGH);
        digitalWrite(sim3Pin, LOW);

if (serialData =='7'){
      digitalWrite(pin43, HIGH);}
      digitalWrite(pin43, LOW);
if (serialData =='8'){
      digitalWrite(pin34, HIGH);}
      digitalWrite(pin34, LOW);

      if ((analog4Value > threshold) and (serialData =='8')){
        digitalWrite(sim4Pin, HIGH);
        digitalWrite(sim4Pin, LOW);

if (serialData =='9'){
      digitalWrite(pin44, HIGH);}
      digitalWrite(pin44, LOW);}

if (serialData =='A'){
      digitalWrite(pin35, HIGH);}
      digitalWrite(pin35, LOW);

      if ((analog5Value > threshold) and (serialData =='A')){
        digitalWrite(sim5Pin, HIGH);
        digitalWrite(sim5Pin, LOW);

if (serialData =='B'){
      digitalWrite(pin45, HIGH);}
      digitalWrite(pin45, LOW);

if (serialData =='C'){
      digitalWrite(pin36, HIGH);}
      digitalWrite(pin36, LOW);

      if ((analog6Value > threshold) and (serialData =='C')){
        digitalWrite(sim6Pin, HIGH);
        digitalWrite(sim6Pin, LOW);

if (serialData =='D'){
      digitalWrite(pin46, HIGH);}
      digitalWrite(pin46, LOW);

if (serialData =='E'){
      digitalWrite(pin37, HIGH);}
      digitalWrite(pin37, LOW);

      if ((analog7Value > threshold) and (serialData =='E')){
        digitalWrite(sim7Pin, HIGH);
        digitalWrite(sim7Pin, LOW);

if (serialData =='F'){
      digitalWrite(pin47, HIGH);}
      digitalWrite(pin47, LOW);

Can you move your code tags a little around so your question is not inside the code block?