Using two SPI devices on Pro-Mini (LoRa and 4-20mA transmitter)

Hello forum,

I've been using the Ra-02 LoRa modules for a while now, very impressed by its performance. In a recent device I made I combined a 4-20mA transmitter with the LoRa module on an UNO. This specific mA transmitter uses I2C to connect to the UNO so this together with the LoRa module my device works well together.

Lately I've been moving more towards pro-minis, in order to cut out the need for logic level converters for the LoRa radio. then I bought a different mA transmitter that also runs off 3.3V. Challenge is that this mA transmitter uses SPI, same as the LoRa radio. Both devices work beautifully when connected seperately, but I'm having a hard time getting them to work together on the same pro-mini.

Radio: https://wiki.ai-thinker.com/_media/lora/docs/c048ps01a1_ra-02_product_specification_v1.1.pdf

3.3V mA transmitter: https://download.mikroe.com/documents/add-on-boards/click/4-20ma/4-20ma-click-manual-v100.pdf

The LoRa default setup: NSS - D10, MOSI - D11, MISO - D12, SCK - D13.

The mA transmitter default setup: SDI - D11, SCK - D13, CS - D10. For CS I just changed the sketch to CS = D08, changed the lead onto D08, and the mA transmitter works fine like this..... Until I initialise the LoRa radio. Then the transmitter stops responding to commands - this is with the LoRa SS pin connected to D10

The LoRa library () allows to change the SS pin to whatever you want, and I've changed it to D07 as a test, and the radio works fine with this setting.

But when I run the code with LoRa and mA transmitter together, no joy. When I comment out the LoRa initialisation the mA transmitters works fine. When I do initialise and use the radio it works fine, but mA transmitter does not work.

I read somewhere that when using multiple SPI devices I need to keep D10 as an output, even when doing this I can't get both devices to work on this board.

I suspect there's something simple which I'm missing - can anyone nudge me in the right direction?

Thanks,
Hein

My sketch:

#include <LoRa.h>
#include <avr/wdt.h>

unsigned long TXinterval = 3000;
long lastTX = -TXinterval;

const int multiplier = 1000;
const int slaveSelectPin = 8; // click SHIELD SLot 1 = D10, Slot 2 = D9
const float minCurrent = 4 * multiplier;
const float maxCurrent = 20 * multiplier;
const float minScaledCurrent = 800; //Not sure where this value came from
const float maxScaledCurrent = 4095; //four MSB of the MCP4921 are control bits, leaving 12 for data

float mAScaledF;
int mAScaled;
int mAScaledInt;
int mA;
byte mAScaled_Hi;
byte mAScaled_Lo;

void setup()
{
  wdt_enable(WDTO_8S);
  Serial.begin(9600);
  pinMode (slaveSelectPin, OUTPUT);

pinMode (10, OUTPUT);
  
  pinMode (11, OUTPUT);
  pinMode (13, OUTPUT);
  digitalWrite(slaveSelectPin,HIGH);
  digitalWrite(13,LOW);
  digitalWrite(11,LOW);
//  setupLoRa();
}

void loop()
{

  wdt_reset();

  if (millis() - lastTX > TXinterval)
    sendPacket();


  for (mA = minCurrent; mA <= 10000; mA = mA + 1000 )   //The scaling formula below scales 4-20   to 800-4095  
  {
    mAScaledF = ((((float)mA - minCurrent) / (maxCurrent - minCurrent)) * (maxScaledCurrent - minScaledCurrent) + minScaledCurrent);
    mAScaled = (int) mAScaledF;
    mAScaled_Hi = (mAScaled >> 8) &0x0F;
    mAScaled_Hi |= 0x30;
    mAScaledInt = (mAScaled_Hi << 8);
    mAScaled_Lo = mAScaled;
    mAScaledInt |= mAScaled_Lo;
  
    digitalWrite(13,LOW);
    digitalWrite(11,LOW);
    digitalWrite(slaveSelectPin,LOW);
    shiftOut(11, 13, MSBFIRST, (mAScaledInt >> 8));  
    shiftOut(11, 13, MSBFIRST, mAScaledInt);
    digitalWrite(slaveSelectPin,HIGH);

    Serial.print("mA = ");
    Serial.println((float)mA / multiplier, 3);
    delay(1000);
  }
}


void setupLoRa()
{
  LoRa.setPins(7, 9, 2);
  
  if (!LoRa.begin(433E6))      // CUSTOMISE
    Serial.println("LoRa Reset..FAILED!");

  else
    Serial.println("LoRa Reset..OK");

  LoRa.setSpreadingFactor(12); 
  LoRa.setSyncWord(0xF4);           // ranges from 0-0xFF, default 0x34, see API docs
  delay(100);
}


void sendPacket()
{ 
  String test = "TESTING123";
  Serial.print(F(">>> SENDING: "));
  Serial.println(test);

  LoRa.beginPacket();
  LoRa.print(test);
  LoRa.endPacket();

  LoRa.end();
  setupLoRa();
}
    digitalWrite(13,LOW);
    digitalWrite(11,LOW);
    digitalWrite(slaveSelectPin,LOW);
    shiftOut(11, 13, MSBFIRST, (mAScaledInt >> 8)); 
    shiftOut(11, 13, MSBFIRST, mAScaledInt);
    digitalWrite(slaveSelectPin,HIGH);

You must not use the SPI pins for bit-banging while SPI is in use. Why don't you use the SPI interface for the 4-20ma module too?

I have to admit that I don't really know what is happening here:

digitalWrite(13,LOW);
    digitalWrite(11,LOW);
    digitalWrite(slaveSelectPin,LOW);
    shiftOut(11, 13, MSBFIRST, (mAScaledInt >> 8));
    shiftOut(11, 13, MSBFIRST, mAScaledInt);
    digitalWrite(slaveSelectPin,HIGH);

How would I change this code and the wiring to do as you suggest? I currently have both LoRa and mA transmitter sharing digital pins 11 and 13. The mA transmitter does not connect to MISO, so only the LoRa radio is connected to pin 12. Then I have CS for the LoRa radio on 7 and SS for the mA transmitter on 8.

I have to admit that I don't really know what is happening here:

Copying code you don't understand to another sketch is not a good idea...

I guess the following code is about a replacement for that part:

  digitalWrite(slaveSelectPin, LOW);
  SPI.transfer(mAScaledInt >> 8);
  SPI.transfer(mAScaledInt & 0xFF);
  digitalWrite(slaveSelectPin, HIGH);

And remove the pinMode() and digitalWrite() calls for pins 11 and 13 from the setup().

Thanks for the code - works beautifully!!!