What I wanted to learn, and why it interested me: I wanted to learn how to use radio communication between two arduinos because I think it's a really interesting capability that can be used for projects requiring wireless communication.
Final outcome: The intention of the final build was to have the ultrasonic ranger (the transmitter) detect the presence of a person, which triggers the movement of a servo motor (the receiver), which represents a physical outcome of the radio signal (in this project, it's the cat waving, but I think it'd be cool to use a motor to open a door, or move a lever etc).
Images of final creative/exploratory output
Cat on the motor waves when radio receiver receives signal from radio transmitter (distance from ultrasonic ranger).
Radio receiver and radio transmitter side by side.
Process images from development towards creative/exploratory output
When I switched components from the LED and potentiometer to the servo motor and ultrasonic ranger, information no longer seemed to be sending from the transmitter to the receiver. As part of the debugging, I tried sending information from the potentiometer (which I knew worked) to the servo motor.
Process and reflection:
The process for the creative assignment was a bit more confusing than the technical assignment I chose, despite the structure of the code being pretty much the same. I was able to get the components running separately, and I knew that the radio modules weren't broken since they worked in the technical assignment, so I was much at a loss in thinking about other debugging approaches I could take. Since radio communication is not physically observable, it was a bit tricky to find what potentially was messing with the transmission of the signal, whether that was the distance the ultrasonic ranger was reading at (strangely this did affect it), the radio address, the PA level, or the baud rate (serial.begin). This project taught me that even if I think the code should work and is theoretically right, I should still try to experiment with it, which would both open new approaches to debugging and also help me understand unfamiliar lines of code. Overall, I did not find the process to be very rewarding in the end because it felt so finnicky, but I am glad I tried using the radio communication modules and I think it opens a lot of doors for future projects!
Technical details
electrical schematics for radio transmitter and radio receiver
//transmitter code (ULTRASONIC RANGER)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <NewPing.h>
#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "12345";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_LOW);
radio.stopListening();
Serial.begin(9600);
}
void loop() {
int sendVal = sonar.ping_cm();
radio.write(&sendVal, sizeof(sendVal));
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
//Serial.print("Ping: ");
Serial.println(sendVal); // Send ping, get distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");
}
//receiver code (SERVO MOTOR)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo gaugeMotor;
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "12345";
const int MOTORPIN = 3;
int sendVal, motorPos;
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.startListening();
radio.openReadingPipe(1, address);
gaugeMotor.attach(MOTORPIN);
}
void loop() {
if (radio.available()) {
radio.read(&sendVal, sizeof(sendVal));
Serial.println(sendVal);
motorPos = map(sendVal, 0, 50, 10, 170);
gaugeMotor.write(motorPos);
delay(10);
}
}