Go Down

Topic: Radio stole my pins  (Read 213 times) previous topic - next topic

melongrenade

Hello
 
Wasn't sure where to post this maybe general ? but seemed right as I came across Wire, I2C .
 
The issue I'm running into is my project uses radio to send data and has tied up the pins I'd normally use for the screen . Thinking using two nanos  ( one for radio and one for screen) with I2C passing sensor data ?  Have tested out some of the examples between two  Nano (clones) but nothing seems to come through .  
 
Pin layout
 
A5 - A5
A4 - A4
GND - GND
 
Sketch used
 
Code: [Select]
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}


 
Code: [Select]
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}


 
Have read that the Arduino  uses it's own pull ups , so we don't need to add resistors to the VCC ?
Near the point where things are getting out of my depth .  Would there be a better way of doing this or should I keep trying to get I2C working ?
 
Thanks in advance

Robin2

I have no idea what you mean by "radio stole my pins" as you have not provided any details.

This Arduino to Arduino I2C Tutorial may be of interest.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

melongrenade

#2
Nov 03, 2020, 08:39 pm Last Edit: Nov 03, 2020, 08:40 pm by melongrenade
Hey Robin.

Sorry the website was buggy on sign in and yes I'm not the best at describing things but will try.

I can't seem to use both a nRF24L01 and 1.44 TFT LCD screen at the same time on one nano. The radio seems to use the pins i'd normally use with the sreen. So have been looking for a work around, advice or to be told I can just change the pins on either the screen or the radio.

I've also found out that there's an issue with cloned arduinos with I2C as it seems to be missing the chip\pins.....?
Have been trying an older Uno clone with a ATMEGA328 chip and another chip on breadboard that has allowed me to use I2C. 

Hope this helps better explain things and if anyone else is having issues with clones.

Thanks in advance





Robin2

I can't seem to use both a nRF24L01 and 1.44 TFT LCD screen at the same time on one nano.

You still have not said what pins are causing the problem.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

melongrenade

#4
Nov 03, 2020, 09:57 pm Last Edit: Nov 03, 2020, 10:09 pm by melongrenade
From what I understand the Nano and Uno use pins A4\SDA and A5\SCL for I2C . The issue I had with running I2C on the Nano clone was cleared up after using just the ATMEGA328 chip. I'm guessing the Nano or new Uno clones are missing a way to use I2C or Wire.h

The issue with the screen and the Radio sharing pins 

TFT screen pins

5V  - 5V
GND - GROUND
GND -
NC  -
NC  -
LED - 5V
SCL - 13
SDA - 11
RS  - 8
RST - 9
CS  - 10

nRF24L01 pins

VCC - 3.3V
GND - GND
CSN - 8
CE - 7
MOSI - 11
SCK - 13
IRQ -
MISO  - 12

I know I can swap pins 7 & 8 for the Radio but unsure on pins 11 & 13 as both seem to want them pins.
Sorry would rather show the tell. If we can swap some pins around I can ignore I2C and just send values straight from radio to the screen. Not sure but A4 and A5 looked good but might have the same issue with the Nano being a clone or if the screen can use Analog or if I should ignore that it says SCL & SDA ?










jremington

#5
Nov 03, 2020, 11:43 pm Last Edit: Nov 03, 2020, 11:44 pm by jremington
Quote
I'm guessing the Nano or new Uno clones are missing a way to use I2C or Wire.h
No, they work fine with I2C.

You seem to be confused between the I2C bus, which uses pins SCL and SDA, and the SPI bus, which uses MOSI, MISO, SCK and a device select.  You need to sort out which device is using which bus. Both buses can be shared by several devices, or they can be used independently.

groundFungus

#6
Nov 04, 2020, 03:38 am Last Edit: Nov 04, 2020, 10:27 pm by groundFungus
Which exact TFT LCD do you have?  The TFT LCD screens that I have all use the SPI bus.

The TFT and the rf24 both use the SPI bus, not I2C.  They share pin 13 (SCK) and pin 11 (MOSI)
(and pin 12 (MISO) if used).

The TFT also needs a CS (chip select), RS (register select) and RST (reset) pins.  Those pins can be specified in the constructor for most libraries.

The rf24 radio needs a CE (enable)and CSN pin.  Those pins can be specified in the constructor for the RF24 library and they must be different pins.

Here, as an example, is the constructors for my setup with a 1.44" TFT and a rf24 radio.
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte TFT_CS = 6;
const byte TFT_RST = 7;
const byte TFT_DC = 8;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);


You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

melongrenade

#7
Nov 04, 2020, 10:18 pm Last Edit: Nov 04, 2020, 10:20 pm by melongrenade
Apologies for the late reply


Quote
You need to sort out which device is using which bus. Both buses can be shared by several devices, or they can be used independently.
Hey jreminton

Alot of these things are going over my head at the moment as this is just a hobby for under a year . It's really tricky to understand what I'm asking alot of the time and embracing to ask questions.




Quote
Which exact TFT LCD do you have?  The TFT LCD screens that I have all use the SPI bus.
MakerHawk TFT LCD Screen 1.44 inches TFT LCD Module, 128x128 SPI, Picture Graphic Color Screen, 51 STM32
Hope that helps ?


Quote
Here, as an example, is the constructors for my setup with a 1.44" TFT and a rf34 radio.
Hey thanks again , will try this once I've got some time.
While I'm here would you guys mind looking at my code and telling me what's missing or wrong ? I've confused myself reading too many diffrent examples and trying to put it together - Be warned it's choppy and missing alot but wanted the master to ask the slave for Specific data and have the slave return the correct stored values.

Got confused and had to stop

Master

Code: [Select]
// Want the master to send request for diffrent values from the slave

#include <Wire.h>

int CheckSend[3] = {0, 1, 2};
int FirstVal = 0;
int SecondVal = 0;
bool NewData = false; // Groundfungus taught us this thank you

void GetData();
void ShowData();

void setup() {

  Wire.begin();   //Master
  Serial.begin(9600);

}

void loop() {

  GetData();
  ShowData();

}

void GetData() {

 
     Wire.beginTransmission (1);    // Transmit to slave 1
     Wire.write (CheckSend[0]);
     Wire.endTransmission();
     Serial.println("1st Request sent");
     delay (10);
     FirstVal = Wire.read();
     Wire.beginTransmission (1);    // Transmit to slave 1
     Wire.write (CheckSend[1]);
     Wire.endTransmission();
     Serial.println("2ed Request sent");
     delay (10);
     SecondVal = Wire.read();
     
}

void ShowData() {

     Serial.println("1st Value");
     Serial.println(FirstVal);
     Serial.println("2ed Value");
     Serial.println(SecondVal);
   
}


Slave
Code: [Select]
#include <Wire.h>

int RequestCheck = 0;
int Data[2] = {100,200};

void SendData();
bool NewData = false; // lernt this from CaveFungus thank you

void setup() {

  Wire.begin(1);                // Start as Slave 1 ?
  Wire.onReceive(requestEvent);

}

void loop() {

   
    SendData();

}

void requestEvent() {

     RequestCheck = Wire.read();
     NewData == true;
}

void SendData() {

     if (RequestCheck == 0 and NewData == true)

        {
          Wire.write (Data[0]);
          delay(50);
          NewData == false;
        }
     else if (RequestCheck == 1 and NewData == true)

        {
          Wire.write (Data[1]);
          delay(50);
          NewData == false;
        } 
     else
        {
          // can I leave this blank to skip or will this cause problems down the line ?
        }

}





Thanks again in advance and for being patient with me :)






groundFungus

#8
Nov 04, 2020, 10:28 pm Last Edit: Nov 04, 2020, 10:31 pm by groundFungus
The code that I posted is for the constructors for a 1.44" SPI 128x128 TFT display and rf24 radio.  

Neither TFT or the RF24 uses I2c (the Wire library).  So none of your code is actually any good unless you are transferring data via I2C.

Please explain what you want to do.

How to use the rf24 radios is covered in Robin2's simple rf24 tutorial.

The TFT can use the Adafruit_ST7735 library (with the Adafruit_GFX if you want graphics functions) .  That library is available through the library manager.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

melongrenade

#9
Nov 04, 2020, 11:03 pm Last Edit: Nov 04, 2020, 11:07 pm by melongrenade
Quote
The code that I posted is for the constructors for a 1.44" SPI 128x128 TFT display and rf24 radio.
Thanks again for lending us your code. will run it once I have a few moments. Will save me alot of headaches .

Quote
Please explain what you want to do.
I've done the radio with help from this site - thanks guys :) but since I'm here may aswell learn alittle Wire.h

What I'm trying to get it to do is get a master to request certain data from another arduino and have the slave fuigure out the correct value thats been requested and send it back .



Arduino - Arduino I2C


Master - Hey I need the value for LDR 1 please
Slave    - oh ok let me look , here it is (Data[0])
Master - Hey I need LDR 2 value please



Have tryd before and got junk data inbetween so thought IF statments would control things - unsure if that's the best way but gave it a go.








groundFungus

#10
Nov 04, 2020, 11:06 pm Last Edit: Nov 04, 2020, 11:09 pm by groundFungus
So what are we to help with?  Getting the TFT and rf24 to work together or getting I2C communication to work?  I would say to work on one at a time.

You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

melongrenade

Lets go with TFT and rf24 as this would be a life saver . Was only worried I posted in the wrong place .

Just looking at the bord now and all the pin outs I have . As jreminton brought up I can place more then one thing on some of the lines and this will cause no issues ?

nRF24l01

CE   - 9
CSN  - 10
MOSI - 11
SCK  - 13
MISO - 12

TFT Screen

SCL - 13
SDA - 11
RS  - 8    // I'm thinking this is DC....?  
RST - 7  
CS  - 6


Is this correct ?
How do people know what pin to use is this something in the library that you use ?

groundFungus

#12
Nov 05, 2020, 12:34 am Last Edit: Nov 05, 2020, 12:36 am by groundFungus
Quote
Is this correct ?
Yes, that is how my example code is set up.

Quote
RS  - 8    // I'm thinking this is DC....? 
Yes and on some it is labeled A0.

Quote
How do people know what pin to use is this something in the library that you use ?
I guess that is a matter of experience.  You use what pins are free that will work.

Here is my full, working, test code using rf24 radios connected to 2 Unos and the receiving Uno has a 1.44" SPI TFT attached.  The sender Uno has 2 LDRs connected to pins A0 and A1.  The analog input (A0 and A1) pins have their internal pullups enabled (instead of external load resistors for the LDRs).  The LDR values are put into a struct and the struct is transmitted to the sender.  The sender receives the values (struct) and displays the values on the TFT. 

Sender:
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues
{
  int ldr_1;
  int ldr_2;
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{

   Serial.begin(9600);
   Serial.println("SimpleTx Starting");
   pinMode(LDR1, INPUT_PULLUP);
   pinMode(LDR2, INPUT_PULLUP);
   radio.begin();
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      prevMillis = millis();
   }
}

//====================

void send()
{
   ldrValues.ldr_1 = analogRead(LDR1);
   ldrValues.ldr_2 = analogRead(LDR2);
   radio.write( &ldrValues, sizeof(ldrValues) );
}


Receiver:
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte TFT_CS = 6;
const byte TFT_RST = 7;
const byte TFT_DC = 8;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{
   Serial.begin(9600);
   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
   tft.initR(INITR_144GREENTAB);
   //tft.initR(INITR_BLACKTAB);
   tft.fillScreen(ST7735_BLACK);
   tft.setCursor(10, 10);
   tft.setTextColor(ST7735_BLUE);
   tft.setTextSize(2);
   tft.println("Read LDRs");
   delay(3000);
   tft.fillScreen(ST7735_BLACK);
   tft.setTextColor(ST7735_RED);
   tft.setCursor(5, 30);
   tft.print("LDR1 ");
   tft.setCursor(5, 60);
   tft.print("LDR2 ");
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{

   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      tft.fillRect(70,30,50,16,ST7735_BLACK);
      tft.setCursor(70, 30);
      tft.setTextColor(ST7735_RED,ST7735_BLACK);
      tft.print(ldrValues.ldr_1);
      tft.fillRect(70,60,50,16,ST7735_BLACK);
      tft.setCursor(70, 60);
      tft.setTextColor(ST7735_RED,ST7735_BLACK);
      tft.print(ldrValues.ldr_2);

      newData = false;
   }
}
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

melongrenade

Was just trying a simple hello script after getting the screen working but HOLY MOLY I didn't expect you to do all that :o Not sure what to say but thank you for being a MASSIVE help , you've saved me alot of time and greif - again  :smiley-cool:  

Just used it and after abit of wiggling the cables (cheap breadboard) Everything worked.

I have placed capacitor on the line to try and stabilise it but will it work better once everything is solderd down or should I expect the same hit and miss with these little radios ?












groundFungus

#14
Nov 05, 2020, 01:50 am Last Edit: Nov 05, 2020, 01:57 am by groundFungus
I think that the biggest potential problem with the rf24 radios is the 3.3V power supply.  The 3.3V supply from a Nano or Uno is not always enough.  The 10uF cap across the supply as close to the radios as possible often helps.  My setup is all soldered so the connections are solid and my radios have dedicated 3.3V supplies.

These adapters contain a 3.3V regulator and may increase the reliability of the radios.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

Go Up