Can I set same pin for two devices?

I'm making a project to display Text on P10 Led display using Arduino Uno. I'm also using ethernet shield(enc28j60). But I'm facing issues as both enc28j60 and P10 display require pin 11 & 13. Since Pin 11,12,13 are SPI pins I tried connecting them serially but to no avail. I might be wrong as I'm not much known to hardware yet. Some help would be appreciated.
Code:


#include <SPI.h>
#include <PubSubClient.h>
#include <UIPEthernet.h>
#include <DMD.h>
#include <TimerOne.h>

#include "TimerOne.h"
#include "SystemFont5x7.h"

DMD dmd(1, 1);
int count = 0;
String Var;
String str;
String c;
int slen = 0;
int clen = 0;
char b[8];

const char* mqtt_server = "192.168.1.115";
const int mqtt_port = 1883;
const char* mqttUser = "getparking";
const char* mqttPassword = "playtmbiz";

EthernetClient ethClient;
PubSubClient client(ethClient);
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
unsigned long lastMsg = 0;
const char* post = "loop/";
const char*  fetch = "dmd/";

void ScanDMD()
{
  dmd.scanDisplayBySPI();
}

void callback(char* topic, byte* message, unsigned int length);
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String Car;
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
    Car += (char)payload[i];
  }

  Var = Car;
  publishMessage(fetch, Car, true);
  //Serial.println();
}
void setup()
{

  Serial.begin(9600);
  while (!Serial) delay(1);
  Ethernet.begin(mac);
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
  client.subscribe(fetch);
  Timer1.initialize( 5000 );
  Timer1.attachInterrupt( ScanDMD );
  dmd.clearScreen( true );
}

void reconnect()
{
  if (client.connect("arduinoClientSuperior", mqttUser, mqttPassword))
  {
    Serial.println("connected");
    client.subscribe(post);
  }
  else
  {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 5 seconds");
    delay(1000);
  }
}
void loop()
{
  while (!client.connected())
  {
    Serial.println("Connecting to MQTT...");
    reconnect();
  }
  client.loop();


  delay(1000);
  msgreadcode();
}

void publishMessage(const char* topic, String payload , boolean retained)
{
  if (client.publish(topic, payload.c_str(), true))
    Serial.println("Message publised [" + String(topic) + "]: " + payload);
}
void msgreadcode()
{
  Serial.print("code is working ");
  int slen = 0;
  dmd.clearScreen( true );
  dmd.selectFont(System5x7);
  str = Var;
  slen = str.length() + 1;
  str.toCharArray(b, slen);
  dmd.drawString(1, 1, b, slen, GRAPHICS_NORMAL);
  publishMessage(post, "msg displayed", true);
  delay(2000);
  dmd.clearScreen( true );
}


error

image

The Ethernet module is using the SPI interface, hence its use of those pins, but why does the display need to use the same pins ? Are they hard coded in the library that you are using ?

Yes, I'm using the DMD-master library by freetronics for my project. This is present in the DMD.h file. I even tried changing the pins, renaming the library and then uploading the library into the project but still no positive sign. I even connected ethernet module to the icsp pins on arduino board.

It looks like you could change the pin numbers in those #defines, in fact there are specific references to the other uses of pins 11 and 13

However, the READMO for the library also says

Includes:

  • High speed display connection straight to SPI port and pins.

So it looks like the display uses the SPI interface. If that is the case then you should be able to connect the Ethernet board and the display in parallel to the SPI pins. Have you tried that ?

I've posted the image of connections in post1 itself. I also tried a normal publish message program while the connections were on icsp pins and a basic text display program from dmd where in I changed the pins from library (same as you mentioned) and renamed that library and included it using zipcode. Still it accepts the previous pins only.

#include <Arial14.h>
#include <Arial_black_16.h>
#include <Arial_Black_16_ISO_8859_1.h>
#include <DMD.h>
#include <SystemFont5x7.h>
#include <SPI.h>
#include <TimerOne.h>
#include "SystemFont5x7.h"

String str;
char b[8];

void ScanDMD()
{
  dmd.scanDisplayBySPI();
}
void setup() {
  Serial.begin(9600);
  pinMode(sensorValue, INPUT_PULLUP);
  Timer1.initialize(5000);
  Timer1.attachInterrupt(ScanDMD);
  dmd.clearScreen(true);
}
void loop()
{
Serial.println("running");
    int slen = 0;
    dmd.clearScreen( true );
    dmd.selectFont(System5x7);
    str = "WORKING";
    slen = str.length() + 1;
    str.toCharArray(b, slen);
    dmd.drawString(2, 4, b, slen, GRAPHICS_NORMAL);
    delay(2000);
    dmd.clearScreen( true );
  }

The DMD library is coded to use the SPI library so changing the #defined pins is not going to work

My advice would be to get the display working with the examples from the library with no other hardware connected

Once they work then add the Ethernet shield and test the display again before adding any Ethernet code

I already did try that. All the three hardware together (Arduino UNO+ENC28J60+P10) can run a single project i.e either display example or message publish example efficiently. But when I try it with my code i.e both the devices together it throws error

This is the first mention of an error

Please post the full error message

Note that you do not connect the SPI pins in series but in parallel

error
Sorry my bad, the project is uploading completely fine and I'm receiving error in the output.

This is the pin connection I've made. Please let me know where I'm going wrong. I'm new to this field.

The small portion of the output that you posted relates to the failure to connect to the MQTT broker

Do the PubSubClient examples work when the display is not connected ?

I cannot tell how things are connected from your picture. Please post a schematic of your project. A picture of a hand drawn circuit is good enough

Something ike this......

Hi,

Sorry no.
Please post an image of a hand drawn circuit.
Please include power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:


I'm sorry for the inconvenience caused. I hope this helps

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.