[Solved] Transfer serial monitor readings of a flex sensor to LCD with RF24L01

Hello,

I’m trying to use two RF24L01 modules to connect an Uno with a flex sensor and a MEGA 2560 with an LCD screen. I’d like the readings that I get from the serial monitor for the flex sensor to appear on the LCD.

So far, I’ve followed individual tutorials for each and have successfully got readings from the flex sensor in the serial monitor and used the RF24 modules to send a static message reading “data” to the LCD and have it flash on screen. I’ve searched around but I’m at a loss about how to insert the readings properly into the transmitter code. I suspect I’m not using the right variable but I’m sure how to phrase it for finding it myself.

Here’s is the void loop for my transmitter code where I think the problem is:

void loop() {
  // Interpret values from the flex sensor:
  data = analogRead(flexs);
  // Create an array for the data to be displayed in (this is the line that I think needs to change):
  const char text[] = "(data)";
  // Communicate that to the receiver module:
  radio.write(&text, sizeof(text));
  delay(1000);
}

I’ve attached my transmitter and receiver codes. Hope you can help me out!

Here’s the datasheet for the flex sensor I’m using, just in case:

Thanks,

SEM

TransmitFlexToLCDForumHelp.ino (1.53 KB)

ReceiveFlexToLCDForumHelp.ino (1.94 KB)

Nice to see code tags being used but always attache the entire code. Note that several windows showing code tagged codes are possible.

I see! Here’s the rest:

Transmitter

/*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
* The circuit:
* Flex Sensor:
  * ends to +5V and ground 
  * 10K resistor in series
  * Pin to A0 between Flex sensor and resistor
* RF24 VCC pin to 3.3V
* RF24 GND pin to GND
* RF24 CSN pin to digital pin 8
* RF24 CE pin to digital pin 7
* RF24 SCK to digital pin 52 (Mega)/13 (Uno)
* RF24 MOSI to digital pin 51 (Mega)/11 (Uno)
* RF24 MISO to digital pin 50 (Mega)/12 (Uno)
*/
// Include the libraries necessary for this side of the communication:

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

// Initialise the libraries and set pins:

RF24 radio(7, 8); // CE, CSN
int flexs = A0;
int data = 0;

// Determine the address that the RF24 module uses to communicate:

const byte address[6] = "00001";

void setup() {
  // Have the RF24 module begin setting up for transmission:
  Serial.begin(9600);
  pinMode(flexs, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  // Interpret values from the flex sensor:
  data = analogRead(flexs);
  // Create an array for the data to be displayed in:
  const char text[] = "(data)";
  // Communicate that to the receiver module:
  radio.write(&text, sizeof(text));
  delay(1000);
}

/* Transmitter code adapted from Dejan Nedelkovski, www.HowToMechatronics.com
 * Flex Sensor code adapted from Engr Fahad http://www.electroniclinic.com/arduino-flex-sensor-or-bend-sensor/
 */

Receiver

/* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
* Library: David A. Mellis/LCD (In the public domain on the arduino website)
* The circuit:
* LCD RS pin to digital pin 5
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
  * ends to +5V and ground
  * wiper to LCD VO pin
* RF24 VCC pin to 3.3V
* RF24 GND pin to GND
* RF24 CSN pin to digital pin 8
* RF24 CE pin to digital pin 7
* RF24 SCK to digital pin 52 (Mega)/13 (Uno)
* RF24 MOSI to digital pin 51 (Mega)/11 (Uno)
* RF24 MISO to digital pin 50 (Mega)/12 (Uno)
*/
// Include the libraries necessary for this side of the communication:

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

// Initialise the libraries and set pins:

LiquidCrystal lcd(5, 6, 9, 10, 11, 12);
RF24 radio(7, 8); // CE, CSN

// Determine the address that the RF24 module uses to communicate:

const byte address[6] = "00001";

void setup() {
  // Have the RF24 module start listening for the transmission address:
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  
  // Set up the output LCD, ready to receive Elbow Flexion values:
  lcd.begin(16, 2);
  lcd.print("Elbow Flex:");
}
void loop() {
  // Receive incoming data and print to the output LCD:
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    lcd.setCursor(10, 1);
    Serial.print(text);
    lcd.print(text);
    delay(1000);
    lcd.setCursor(10, 1);
    lcd.print("     ");
    delay(250);
  }
}

/* Receiver code adapted from Dejan Nedelkovski, www.HowToMechatronics.com
 * LCD code adapted from Tom Igoe http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

Thanks for the advice :slight_smile:

Well done!
Reading this, it feels strange to me:

  data = analogRead(flexs);
  // Create an array for the data to be displayed in:
  const char text[] = "(data)";

The third line looks strange. You fabricate a string containing the chars (data) , not the contents of data. Maybe a pointer to the array would do better? The "( and )" looks odd to me.

Life is a lot simpler.

Instead of this

void loop() {
  // Interpret values from the flex sensor:
  data = analogRead(flexs);
  // Create an array for the data to be displayed in:
  const char text[] = "(data)";
  // Communicate that to the receiver module:
  radio.write(&text, sizeof(text));
  delay(1000);
}

just do this

void loop() {
  // Interpret values from the flex sensor:
  data = analogRead(flexs);

  // Communicate that to the receiver module:
  radio.write(&data, sizeof(data));
  delay(1000);
}

with an equivalent change on the receiver side.

…R
Simple nRF24L01+ Tutorial

Maybe using the Array "text"? "data" is an int.

Excellent work Robin2 and Railroader! I had emails and alerts on for this post but didn’t get any so I was surprised to see your replies already there.

Replacing (text) for (data) and simplifying the problem line seems to have done the job. I missed a few stray (text)s on the receiver end but found them eventually after a few tries.

Here is the code with the changes reflected:

Transmitter

// Include the libraries necessary for this side of the communication:

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

// Initialise the libraries and set pins:

RF24 radio(7, 8); // CE, CSN
int flexs = A0;
int data = 0;

// Determine the address that the RF24 module uses to communicate:

const byte address[6] = "00001";

void setup() {
  // Have the RF24 module begin setting up for transmission:
  Serial.begin(9600);
  pinMode(flexs, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  // Interpret values from the flex sensor:
  data = analogRead(flexs);
  // Communicate that to the receiver module:
  radio.write(&data, sizeof(data));
  delay(1000);
}

Receiver

// Include the libraries necessary for this side of the communication:

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

// Initialise the libraries and set pins:

LiquidCrystal lcd(5, 6, 9, 10, 11, 12);
RF24 radio(7, 8); // CE, CSN

// Determine the address that the RF24 module uses to communicate:

const byte address[6] = "00001";

void setup() {
  // Have the RF24 module start listening for the transmission address:
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  
  // Set up the output LCD, ready to receive Elbow Flexion values:
  lcd.begin(16, 2);
  lcd.print("Elbow Flex:");
}
void loop() {
  // Receive incoming data and print to the output LCD:
  if (radio.available()) {
    int data = 0;
    char text[32] = "";
    radio.read(&data, sizeof(data));
    lcd.setCursor(10, 1);
    Serial.print(data);
    lcd.print(data);
    delay(1000);
    lcd.setCursor(10, 1);
    lcd.print("     ");
    delay(250);
  }
}

The weirdness of the “(data)” was just me having a stab in the dark, trying to get the results that I wanted with only what I’d seen from tutorials to try to get there. It was literally printing = (data) on the screen!

Thanks so much for your help!