Jdy40 Arduino Example Best
| JDY-40 Pin | Arduino Pin | Notes | | :--- | :--- | :--- | | VCC | 3.3V (Not 5V!) | Using 5V will eventually kill the module. | | GND | GND | Common ground is mandatory. | | TX | Pin 2 (SoftwareSerial) | Do not use hardware Serial (Pins 0/1) for data. | | RX | Pin 3 (SoftwareSerial) | Use a voltage divider (3.3V logic is safer). | | SET | Pin 4 (Optional) | Pull LOW to enter AT command mode. |
Critical Best Practice: Add a 100µF capacitor across VCC and GND on the JDY-40. This filters noise from the Arduino’s regulator and doubles the effective range.
This example transmits temperature and humidity with error checking.
Transmitter (Sensor Node):
#include <SoftwareSerial.h> #include <DHT.h>SoftwareSerial jdy40(2, 3); DHT dht(7, DHT11);
void setup() jdy40.begin(9600); dht.begin();
void loop() float temp = dht.readTemperature(); float hum = dht.readHumidity(); jdy40 arduino example best
// Create structured packet: <START;LENGTH;DATA;CHECKSUM> String payload = String(temp) + "," + String(hum); int checksum = temp + hum; // Simple numeric checksum String packet = "<" + String(payload.length()) + ";" + payload + ";" + String(checksum) + ">";
jdy40.println(packet); // println adds newline as delimiter delay(5000); // Send every 5 seconds
Receiver (Base Station):
#include <SoftwareSerial.h>SoftwareSerial jdy40(2, 3); String buffer = "";
void setup() Serial.begin(9600); jdy40.begin(9600); | JDY-40 Pin | Arduino Pin | Notes
void loop() while (jdy40.available()) char c = jdy40.read(); if (c == '\n') // End of packet processPacket(buffer); buffer = ""; else buffer += c;
void processPacket(String packet) // Expected format: <LENGTH;DATA;CHECKSUM> if (!packet.startsWith("<")
Most "bad examples" fail because of power instability. The JDY-40 runs on 3.3V logic. While some modules tolerate 5V, to get the best performance, use a level shifter or a 3.3V Arduino board (like the Pro Mini 3.3V).
For Arduino Uno (5V):
Simpler Method (SoftwareSerial): Use the hardware serial monitor for debugging. void loop() float temp = dht
| JDY-40 Pin | Arduino Uno | Arduino Pro Mini (3.3V) | | :--- | :--- | :--- | | VCC | 3.3V | RAW or VCC | | GND | GND | GND | | TX | Pin 2 (SoftwareSerial RX) | Pin 2 (SoftwareSerial RX) | | RX | Pin 3 (SoftwareSerial TX) | Pin 3 (SoftwareSerial TX) |
Description: A full-duplex communication bridge that allows the user to change the JDY-40 baud rate via Arduino code (removing the need for USB-to-TTL adapters for setup) and provides a "Heartbeat" signal quality indicator.
Why this is the "Best" approach:
void setup() Serial.begin(9600);
void loop() if (Serial.available()) String data = Serial.readStringUntil('\n'); Serial.print("Received: "); Serial.println(data);
That’s it. No library. No complex config. It just works.
Out of the box, the JDY-40 works. But to eliminate interference and maximize range, you must configure it via AT commands.
To enter AT mode: