sending a analog read hc12

hey. What im trying to do is create a telemetry system for my drone using the hc-12 communication module. What i want to do is send an analogue read value across to the reciever where it can be converted to a voltage so i know how much charge the batteries have. My issue is that the hc12 only sends 1 character at a time so you cant put hc12.Write("1024") for example. How can i split it us so it can be re assembled at the other end? thanks, George

My issue is that the hc12 only sends 1 character at a time so you cant put hc12.Write("1024")

Sure you can. Well, using write(), not Write().

Not that sending a hardcoded string makes any sense.

   int someVal = analogRead(somePin);
   hc12.println(someVal); // will send the value AND a carriage return and line feed.

On the other end, you read and store data in a NULL terminated array of chars, until the carriage return arrives. Then, you convert the string to an int using atoi(), and reset the string (by putting the NULL back in position 0).

Do you want to know the on-board battery voltage of your drone?

What is the nominal (full charge) voltage of your on-board battery?

What is the final voltage of the on-board battery? (Final voltage is that voltage below which the battery
would not be allowed to discharge.)

Which MCU are you using in your drone. Is it ATmega328P?

I The hc12 can only send 1 character at a time. In the example code it reads in from the serial line character at a time and sends it. when i analog read it i get say "1005" and i don't know how to split it up so it can be sent character by character and then re assembled. The drone transmitter is an arduino nano and the battery is a 12 v lipo but the analog read goes through a voltage divider so it doesn't blow up.

when i analog read it i get say "1005"

Bullshit. The analogRead() function does NOT return a string. You get something like 1005.

i don't know how to split it up so it can be sent character by character

Well, fortunately, the print() method DOES.

and then re assembled.

Well, you need to provide some help for that process BEFORE it makes sense to tackle that end.

What would YOU make of "1001100099910004326554334657543"?

What would you make of "1001,1000,999,1000,432,65,54,33,465,754,3,"?

@georgegohl888

You may play around with the following setup to have some of the answers of your questions like:
1. How to transfer Battery Voltage like 1005 from NANO to UNO via HC12 Radio Module.
2. How to re-assemble the received data to get back the original data/Battery Voltage.

Operating Procedures:
1. Upload the following sketch into UNO-1. Bring in the Serial Monitor-1 at 9600 baud.

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12's STX Pin, HC-12's SRX Pin
void setup() 
{
  Serial.begin(9600);             // Harware Serial port to computer
  HC12.begin(9600);               // Software Serial port to HC12
}

void loop() 
{
  while (HC12.available())          // If HC-12 has data
  {        
    Serial.write(HC12.read());      // Send the data to Serial Monitor
  }
}

2. Upload the following sketch into NANO. This program of NANO acquires analog voltage on channel A0 at 5-sec interval, and sends it to UNO via HC12 Radio Module. For transmission of data, the program has used HC12.print() method.

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

unsigned long int pmillis;
void setup()
{
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  pmillis = millis();
}

void loop()
{
  while (millis() - pmillis < 5000)
    ;
  pmillis = millis();
  unsigned int x = analogRead(A0); //4.0V = 0x0333
  HC12.print(x);
  HC12.println();
  //while(1);

}

3. Use a jumper wire and connect 3.3V (3V3) point (it is physically 3.63V; also 5V is 4.7V) of the NANO with A0-pin of NANO.

4. Press the RESET button of NANO. Check that around 791 (decimal) has appeared on the Serial Monitor-1 of UNO.

Why and how is it 791
In the sketch of NANO, there is an instruction unsigned int x = analogRead(A0) which has stored around (theoretically) 0x0317 [(1024/4.7)*3.63 = 791] into variable x.

The .print() method/function reads the value of x, uses %10 and /10 operators (my understanding) and find the indices 7, 9, 1 for the corresponding decimal value of 791.

After that, the .print() method writes 0x37 (ASCII code of 7), 0x39 (ASCII code of 9), and 0x31 (ASCII code 1) one after another into HC12 Module.

And thhen, the HC12 module transmits the data using asynchronous serial transmission protocol.

How to get 3.63V back from the received data: 0x37, 0x39, and 0x31?
Reverse process!
Extract 791 from 0x37, 0x39, and 0x31
Evaluate (791*4.7)/1024 to get = 3.63.

PaulS:
Bullshit. The analogRead() function does NOT return a string. You get something like 1005.
Well, fortunately, the print() method DOES.
"?

I meant int or whatever analog read returns

and thankyou @GolamMostafa Ill try that next time i'm working on it :slight_smile: