Breadboard(Full Size, 830 tie points). 2 ea
Arduino Nano Microcontroller 2 ea
SSD1306 OLED Display 2 ea
100 uF capacitor. 2 ea
HC-SR04 Ultrasonic Sensor 1 ea
LED, 10 mm 2 ea
Resistor, 220 ohm 2 ea
9V Battery or Wall adapter 2 ea
9V Battery Barrel Jack Clip 2 ea
22 gauge solid wire, Black, Red, white, yellow, blue, green, orange
Cardboard
Keep wires as short and nead as possible
Use
Red for +5V
Black for Ground
Orange for 9V
Blue, green, yellow, white for signals
Run longer wires underneath(see below) to create a neater installation
When the ultrasonic sensor on the transmitting unit detects an object, it transmits the distance to the receiving unit using a pair of HC-12 radio modules, and lights up an LED if the object is too close.
The distance is also displayed on an SSD1306 OLED display.
9V Battery---> Barrel Jack Adapter----->Vin pin on Nano, GND bus
5V Bus-----red-----5V Bus
GND Bus----blk----GND Bus
HC-SR04 Ultrasonic Sensor:
Vcc---->5V Bus
GND---->GND. Bus
TRIG---->Arduino Pin D4
ECHO---->Arduino Pin D5
HC-12 Radio:
Vcc--->5V. Bus
GND--->GND. Bus
Rx--->Arduino Pin 3
Tx--->Arduino Pin 2
100 uF capacitor across 5V and GND
SSD1306 OLED Display:
Vcc---->5V Bus
GND--->GND Bus
SDA--->Arduino A4
SCL---->Arduino A5
LED: GND------>GND Bus
Hi---->220 ohm Resistor---> Arduino Pin 7
Below: Receiving Unit:
The HC-12 radio receives the data sent by the transmitter, and the Arduino Nano displays the received number on an SSD1306 OLED display.
9V Battery---> Barrel Jack Adapter----->Vin pin on Nano, GND bus
5V Bus-----red-----5V Bus
GND Bus----blk----GND Bus
HC-12 Radio:
Vcc--->5V. Bus
GND--->GND. Bus
Rx--->Arduino Pin 3
Tx--->Arduino Pin 2
100 uF capacitor across 5V and GND
SSD1306 OLED Display:
Vcc---->5V Bus
GND--->GND Bus
SDA---->A4
SCL---->A5
LED: GND------>GND Bus
Hi---->220 ohm Resistor---> Arduino Pin 7
<Wire.h>
<Adafruit_GFX.h>
<Adafruit_SSD1306.h>
<SoftwareSerial.h>
Transmitter Unit Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
// --- OLED Display Settings ---
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
// Create the OLED display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// --- HC-12 Radio Settings ---
// SoftwareSerial(RX pin, TX pin)
// Note: Arduino RX (Pin 2) connects to HC-12 TX. Arduino TX (Pin 3) connects to HC-12 RX.
const int hc12RxPin = 3; //RX pin to Arduino Pin 3
const int hc12TxPin = 2;
SoftwareSerial hc12(hc12RxPin, hc12TxPin);
// --- Ultrasonic Sensor Hardware Pins ---
const int trigPin = 4;
const int echoPin = 5;
const int ledPin = 7;
// Variables for calculation
long duration;
int distance;
void setup() {
// Start hardware serial for computer debugging
Serial.begin(9600);
// Start software serial for HC-12 communication (default baud rate is usually 9600)
hc12.begin(9600);
// Initialize HC-SR04 hardware pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer and set up basic text properties
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.display();
}
void loop() {
// 1. Ping the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// 2. Read the echo and calculate distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// 3. LED Logic- light LED if too close
if (distance < 15) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// 4. Transmit over HC-12 Radio
// This sends the data out into the airwaves
hc12.println("<Dist: ");
hc12.println(distance);
hc12.println(" cm>");
Serial.print("Sent: <Distance:");
Serial.print(distance);
Serial.println("cm>");
// 5. Update the OLED display
display.clearDisplay();
display.setCursor(0, 15);
display.print(F("Dist:"));
display.print(distance);
display.print(F("cm"));
// Display a warning message on the screen if too close
if (distance < 15) {
display.setCursor(0, 45);
display.setTextSize(1);
display.print(F("WARNING: TOO CLOSE!"));
display.setTextSize(2);
}
display.display();
// Wait a second before taking the next reading
delay(1000);
}
Receiving Unit Code:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Display Configuration
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // Typical I2C address for 128x64 or 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// HC-12 Pins
const int rxPin = 3;
const int txPin = 2;
SoftwareSerial HC12(rxPin, txPin);
void setup() {
Serial.begin(9600);
HC12.begin(9600);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed. Check wiring."));
for(;;); // Loop forever if display initialization fails
}
// Clear display buffer and show startup message
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Receiver Ready..."));
display.display();
Serial.println("Receiver Ready. Waiting for data...");
Serial.println("-----------------------------------");
}
void loop() {
// Check if the HC-12 has received a full message
if (HC12.available()) {
// Read the incoming string until the newline character is found
String incomingData = HC12.readStringUntil('\n');
// Print the received data to the Serial Monitor
Serial.print("Received: ");
Serial.println(incomingData);
// Update the OLED display with the new data
display.clearDisplay();
// Print header
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("Incoming Data:"));
// Print the actual data slightly larger
display.setTextSize(2);
display.setCursor(0, 16);
display.println(incomingData);
// Push everything to the screen
display.display();
}
}
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // RX, TX (Connect HC12 TXD to Arduino Pin 10, HC12 RXD to Arduino Pin 11)
int setPin = 9; // Connect HC12 SET pin to Arduino Pin 9
void setup() {
Serial.begin(9600); // Open serial port to computer
HC12.begin(9600); // Open serial port to HC-12, default baud rate is 9600
pinMode(setPin, OUTPUT);
digitalWrite(setPin, LOW); // Enter AT Command mode
delay(100); // Wait for the module to enter command mode
Serial.println("Enter AT commands:");
}
void loop() {
while (HC12.available()) { // If the HC-12 module sends data
Serial.write(HC12.read()); // Send the data to the Serial monitor
}
while (Serial.available()) { // If the Serial monitor sends data
HC12.write(Serial.read()); // Send the data to the HC-12 module
}
}
The HC-12 transceiver module uses AT commands to configure its operating parameters, such as baud rate, communication channel, and transmit power. To use these commands, the module's SET pin must be pulled to a logic low state.
Arduino Forum +2
How to Enter AT Command Mode
Power the module: Connect VCC and GND pins (operating voltage range 3.2V to 5.5V DC).
Pull the SET pin low: Connect the SET pin to GND. This puts the module into AT command mode.
Send commands: Send AT commands to the module's RXD pin using a serial terminal (default baud rate of 9600 bps for the configuration interface).
Exit command mode: Release the SET pin (or set it to logic high) to return the module to its normal operating mode with the new settings.
Arduino Forum +4
List of Common AT Commands
Commands are case-sensitive and must be in uppercase. Each command should be followed by a newline character. The module will typically respond with "OK" or "OK+".
YouTube +4
AT: Test command. The module returns OK to confirm it is in command mode and ready.
AT+V: Get firmware version. The module returns its current firmware version, e.g., HC-12_V1.1.
AT+Bxxxx: Set serial port baud rate. Sets the communication baud rate. Available rates include 1200, 2400, 4800, 9600 (default), 19200, 38400, 57600, and 115200 bps.
Example: AT+B9600 returns OK+B9600.
AT+Cxxx: Set wireless communication channel. Select a channel from 001 to 127. The default is 001 (433.4 MHz), with each channel stepping by 400 KHz. Both modules must be on the same channel to communicate.
Example: AT+C021 returns OK+C021.
AT+Px: Set transmission power level. x can be a number from 1 to 8, with 8 being the highest power (20dBm or 100mW) and 1 being the lowest (-1dBm or 0.8mW). The default is P8.
Example: AT+P5 returns OK+P5.
AT+FUx: Set the working/transparent transmission mode. x can be 1, 2, 3 (default), or 4. These modes offer different trade-offs between power consumption, data rate, and range.
Example: AT+FU3 returns OK+FU3.
AT+RX: Query all current parameters. The module returns all current settings in separate OK+... lines.
AT+SLEEP: Enter sleep mode. The module enters a low-power state (~22µA) and cannot transmit data. It exits sleep mode when the SET pin is pulled low again.
AT+DEFAULT: Restore factory default settings. This resets all parameters to their factory defaults (9600 bps baud rate, channel C001, FU3 mode, 20dBm power).
AT+UPDATE: Enter firmware update mode. The module stops responding to other AT commands until it is power-cycled.
Elecrow +4
Notes:
Look closely at this line in your receiver code: String incomingData = HC12.readStringUntil('\n');
This command tells the Arduino to wait and listen until it sees a "Newline" character (\n).
The Problem: If your transmitting Arduino is using HC12.print("Hello");, it never sends that newline character. Your receiver sits there waiting for a \n that never arrives, eventually times out, and drops the data.
The Fix: Ensure your transmitting code uses println() instead of print().
Change: HC12.print("Data"); -> HC12.println("Data");
" Compare cost, ease of setup, and transmission distance of NRF24L01, Reyax, HC-12, and 433 MHz modules for connecting two Arduino Nanos..."
"Write Arduino Code for sending a message between 2 Arduino Nanos using HC-12 radio modules"
"Write a basic Arduino sketch to read distance in cm using an HC-SR04 ultrasonic sensor with trigger connected to Pin 4 and Echo to Pin 5 "
"Would you like me to show you how to add an LED or buzzer that turns on when an object gets too close?"
"Modify the code to display the distance on an SSD 1306 OLED display connected to SDA and SCL"
"Modify the code to turn on an LED connected to Pin 7 if the distance is less than 15 cm, and turn it off if distance is greater than 15 cm"
"Add code to transmit the distance using an HC-12 radio module, with Rx pin connected to pin 3, Tx to Pin 2, using the Software Serial library"