HC-12 not sleeping

Hello all,
I am measuring the current to my HC-12 module and it is steady at 14-15 mA whether in sleep mode or not. I do get an "OK+SLEEP" response from the module so I believe it is receiving the command ok. I am sure it is something really simple but I can't quite spot it. Any help would be greatly appreciated. I can see a drop in current for a split second when I send the sleep command so I think it may be waking up immediately for some reason. Here is stripped-down version of my code:

#include <SoftwareSerial.h>

#define HC12_SET 5
#define HC12_TX 10
#define HC12_RX 11

SoftwareSerial HC12(HC12_TX, HC12_RX);

void HC12_Sleep()
{

	digitalWrite(HC12_SET, LOW);            // Enter command mode
 	delay(100);                             // Allow chip time to enter command mode
 	HC12.print("AT+SLEEP");             	// Send command
 	delay(200);                             // Wait for a response
  	digitalWrite(HC12_SET, HIGH);         // Exit command / enter transparent mode

}

void HC12_Wake()
{
	digitalWrite(HC12_SET, LOW);            // Enter command mode
 	delay(100);                             // Allow chip time to enter command mode
 	digitalWrite(HC12_SET, HIGH);         // Exit command mode
 	delay(100);                             // Delay before proceeding

}
void setup()
{

	pinMode(HC12_SET,OUTPUT);

	HC12.begin(9600);
	HC12_Sleep();
	HC12.end();

}


void loop()
{ 
	HC12.begin(9600);
	HC12_Sleep();
	HC12.end();

	
	delay(5000);
	return;
}

Why did you run a HC12.begin() and .end() in every loop cycle?

Also you never have to call return in the Arduino loop() function.

Try this code and measure the HC-12 current:

#include <SoftwareSerial.h>

#define HC12_SET 5
#define HC12_TX 10
#define HC12_RX 11

SoftwareSerial HC12(HC12_TX, HC12_RX);

void HC12_Sleep()
{

	digitalWrite(HC12_SET, LOW);            // Enter command mode
 	delay(100);                             // Allow chip time to enter command mode
 	HC12.print("AT+SLEEP");             	// Send command
 	delay(200);                             // Wait for a response
  	digitalWrite(HC12_SET, HIGH);         // Exit command / enter transparent mode

}
void setup()
{
	pinMode(HC12_SET,OUTPUT);

	HC12.begin(9600);
	HC12_Sleep();
	
}


void loop()
{  }

Try this

#include <SoftwareSerial.h>

#define HC12_SET 5
#define HC12_TX 10
#define HC12_RX 11

SoftwareSerial HC12(HC12_TX, HC12_RX);

void HC12_Sleep()
{

	digitalWrite(HC12_SET, LOW);            // Enter command mode
 	delay(100);                             // Allow chip time to enter command mode
 	HC12.print("AT+SLEEP");             	// Send command
 	delay(200);                             // Wait for a response
  	digitalWrite(HC12_SET, HIGH);         // Exit command / enter transparent mode

}

void HC12_Wake()
{
	digitalWrite(HC12_SET, LOW);            // Enter command mode
 	delay(100);                             // Allow chip time to enter command mode
 	digitalWrite(HC12_SET, HIGH);         // Exit command mode
 	delay(100);                             // Delay before proceeding

}
void setup()
{

	pinMode(HC12_SET,OUTPUT);

	HC12.begin(9600);
	//HC12_Sleep(); <-- delete this
	//HC12.end(); <-- delete this

}


void loop()
{ 
	//HC12.begin(9600); <-- delete this
	HC12_Sleep();
	//HC12.end(); <-- delete this
	
	delay(5000);
	//return; <-- delete this

    HC12_Wake();

    delay(5000);
}

Hey guys,
Thanks for your replies. I tried both very sketches and neither work. When I call sleep, the current drops to 0.05 mA for a split second, then goes right back to 15 mA. Very frustrating. Same result with three different modules. And when I check the return from AT+SLEEP I get the proper response so I know things are communicating.

I have a new HC-12 on order. Maybe I got a bad batch...but you know you are desperate when you are swapping hardware.

Please show you resulting code

Here it is. Can't get much simpler. I received a new HC12 today. Same result. Thanks alot for looking at this. It must be something very simple...

// HC-12_sleep.ino

#include <SoftwareSerial.h>


#define HC12_SET	5
#define HC12_TX	10
#define HC12_RX	11

SoftwareSerial HC12(HC12_TX, HC12_RX); // HC-12 TX Pin, HC-12 RX Pin


void HC12_Sleep()
{
	digitalWrite(HC12_SET, LOW);           
	delay(100);                         
 	HC12.print("AT+SLEEP");             	
 	delay(200);                            
  	digitalWrite(HC12_SET, HIGH);         

}

void HC12_Wake()
{
	digitalWrite(HC12_SET, LOW);           
 	delay(100);                        
 	digitalWrite(HC12_SET, HIGH);         
 	delay(100);                         

}
void setup()
{

	pinMode(HC12_SET,OUTPUT);


	HC12.begin(9600);
	HC12_Sleep();

}




void loop()
{ 
	HC12_Sleep();
	delay(3000);
	HC12_Wake();
	delay(3000);
	return;

}

You never have to call return in the Arduino loop() function.

As for code - try to add line feed after the command:

By "add a line feed" - I am guessing you meant to the SLEEP string as in "AT+SLEEP\n". That had no effect.
Removing the loop() return had no effect but that makes sense as the return in a void function in C is optional\implied.

I am really stumped. I will see if I can send AT commands directly from the serial monitor and see what that does.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.