Buona sera a tutti,
vorrei far comunicare un arduino mega (trasmettitore) con un arduino uno (ricevitore) tramite nRF24. Ho quindi acquistato questi nRF24 con adattatore 5V.
seguendo alla lettera questo TUTORIAL nRF24 ho cosi fatto i collegamenti:
Arduino MEGA (trasmettitore)
5V
GND
MISO --> 50
MOSI --> 51
SCK --> 52
CE --> 7
CSN --> 8
Arduino UNO (ricevitore)
5V
GND
MISO --> 12
MOSI --> 11
SCK --> 13
CE --> 7
CSN --> 8
ho verificato la correttezza dello SPI di entrambe le schede.
Una volta caricati i file succede che il trasmettitore invia ogni secondo un comando che NON viene letto dal ricevitore.
questi sono i codici caricati.
CODICE TRASMETTITORE
/*
* https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/
* https://github.com/nRF24/RF24
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
bool led = 0;
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
int stato = 0;
void loop() {
//trasmette 0 e 1
Serial.println(stato);
radio.write(&stato, sizeof(stato));
delay(1000);
stato = !stato;
}
il quale mi stampa in seriale questo:
CODICE RICEVITORE
/*
https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/
https://github.com/nRF24/RF24
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
int stato = 0;
const byte address[6] = "00001";
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
for (int i = 0; i < 10; i++) {
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(50);
}
Serial.println("pronto...");
}
void loop()
{
if (radio.available())
{
radio.read(&stato, sizeof(stato));
Serial.println(stato);
if (stato == 1) digitalWrite(6, HIGH);
else digitalWrite(6, LOW);
}
}
il quale mi stampa in seriale:
quindi non riceve nessun segnale..
allego anche una foto di quanto sono vicini i due arduino:
i due arduino sono collegati nello stesso pc. Ho letto che i vari colori all'interno del codice dell'IDE sono puramente estetici, faccio vedere anche nel caso fossero d'aiuto i colori delle varie righe:
il tutto si compila correttamente.
ma...come posso risolvere?
aiutatemi per favore...non riesco ad uscirne più a capo!
EDIT:
ho caricato lo sketch esempio "scanner" della libreria "RF24" nei due arduino.
Questo è quello che mi spunta in seriale:
TRASMETTITORE
RICEVITORE
e questo il codice "scanner":
/*
* Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
* Updated 2020 TMRh20
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
/**
* Channel scanner and Continuous Carrier Wave Output
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
* good channel for your application.
*
* Run this sketch on two devices. On one device, start CCW output by sending a 'g'
* character over Serial. The other device scanning should detect the output of the sending
* device on the given channel. Adjust channel and output power of CCW below.
*
* Inspired by cpixip.
* See http://arduino.cc/forum/index.php/topic,54795.0.html
*/
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 7 & 8
RF24 radio(7, 8);
//
// Channel info
//
const uint8_t num_channels = 126;
uint8_t values[num_channels];
//
// Setup
//
void setup(void) {
//
// Print preamble
//
Serial.begin(115200);
printf_begin();
Serial.println(F("\n\rRF24/examples/scanner/"));
//
// Setup and configure rf radio
//
radio.begin();
radio.setAutoAck(false);
// Get into standby mode
radio.startListening();
radio.stopListening();
radio.printDetails();
//delay(1000);
// Print out header, high then low digit
int i = 0;
while (i < num_channels) {
Serial.print(i >> 4, HEX);
++i;
}
Serial.println();
i = 0;
while (i < num_channels) {
Serial.print(i & 0xf, HEX);
++i;
}
Serial.println();
//delay(1000);
}
//
// Loop
//
const int num_reps = 100;
bool constCarrierMode = 0;
void loop(void) {
/****************************************/
// Send g over Serial to begin CCW output
// Configure the channel and power level below
if (Serial.available()) {
char c = Serial.read();
if (c == 'g') {
constCarrierMode = 1;
radio.stopListening();
delay(2);
Serial.println("Starting Carrier Out");
radio.startConstCarrier(RF24_PA_LOW, 40);
} else if (c == 'e') {
constCarrierMode = 0;
radio.stopConstCarrier();
Serial.println("Stopping Carrier Out");
}
}
/****************************************/
if (constCarrierMode == 0) {
// Clear measurement values
memset(values, 0, sizeof(values));
// Scan all channels num_reps times
int rep_counter = num_reps;
while (rep_counter--) {
int i = num_channels;
while (i--) {
// Select this channel
radio.setChannel(i);
// Listen for a little
radio.startListening();
delayMicroseconds(128);
radio.stopListening();
// Did we get a carrier?
if (radio.testCarrier()) {
++values[i];
}
}
}
// Print out channel measurements, clamped to a single hex digit
int i = 0;
while (i < num_channels) {
if (values[i])
Serial.print(min(0xf, values[i]), HEX);
else
Serial.print(F("-"));
++i;
}
Serial.println();
} //If constCarrierMode == 0
}
magari può tornare utile a qualcuno che ne capisce molto meglio di me