0

I am new to arduino and Xbee these stuff. Trying to send RPM (obtained and calculated by arduino using puulse function) data from arduino to Xbee as analog input to xbee series1.

It seems XBee analog input read a range of 0 volts to 1.2 volts. However, not quite sure how to implement and write the corret code. Following are Arduino code and schematic circuit. Any idea? I really appreciate your help.

int pin = 7;  //7
float rpm;
float duration;
float y;

unsigned long val;
float rps;
float read_rpm;
int pin_out=10;


void setup()
{
   Serial.begin(9600);
  pinMode(pin, INPUT);
  pinMode(pin_out,OUTPUT);
}

void loop()
{
 val =  pulseIn(pin, LOW,70000000);

 duration=(float)val/1000000.00;
 rps= 1.00/duration;
rpm=60.00*rps;

analogWrite(pin_out,(int)rpm);

Serial.print("RPM=\t");

Serial.print(rpm);


Serial.print("\t rps= \t");

Serial.print(rps);

Serial.print("\t dt=\t");

Serial.println(val);

}

enter image description here

  • analogWrite() generates PWM output (oh, PS : except on Mega, but certainly not on Uno), not a purely analog output. And, though I'm not yet familiar with Xbee, I'm not sure you'll achieve analog transmission with it. You could try using some form of DAC, but as suggested below it would be better to send info in digital form. – FredP Oct 23 '14 at 9:57
2

I would suggest you to send bytes instead of "analog value". When you calculate your value, not sure why you use float and then convert it to int, but here is how i would do. Communicate to xbee via serial communication, send bytes to xbee(its even easier than "analog value"). Added if statment to print RPM every second.

Here is my code:

#include <SoftwareSerial.h>
/* Serial communication with Xbee.
On Arduino pin 9 is Rx connect to Xbee Tx, 
pin 8 is Txconnect to Xbee Rx*/
SoftwareSerial xbeeSerial(9, 8); // RX, TX

int pin = 7;  //7
float rpm;
float duration;
float y;

unsigned long val,time;
float rps;
float read_rpm;
int pin_out=10;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(pin, INPUT);
  pinMode(pin_out,OUTPUT);
  Serial.println("I am ready to send some RPM!");

  // set the data rate for the SoftwareSerial port
  xbeeSerial.begin(9600);
  time = millis();
}

void loop()// run over and over
{
 val =  pulseIn(pin, LOW,70000000);

 duration=(float)val/1000000.00;
 rps= 1.00/duration;
rpm=60.00*rps;

/* Sending float to Xbee
Float is 4 byte size*/
serialFloatPrint(rpm);

int floatToInt = (int)rpm;

/* To send INT to XBEE
Int is 2 byte size*/
xbeeSerial.write(highByte(floatToInt));
xbeeSerial.write(lowByte(floatToInt));

/*Print RPM every second*/
If(millis()-time>1000){
  Serial.print("RPM=\t");
  Serial.print(rpm);
  Serial.print("\t rps= \t");
  Serial.print(rps);
  Serial.print("\t dt=\t");
  Serial.println(val);
  time = millis();
 }
}

void serialFloatPrint(float f) {
  byte * b = (byte *) &f;
  //xbeeSerial.print("f:");
  xbeeSerial.print(b[0]);
  xbeeSerial.print(b[1]);
  xbeeSerial.print(b[2]);
  xbeeSerial.print(b[3]);
  /* DEBUG */
  Serial.println();
  Serial.print(b[0],BIN);
  Serial.print(b[1], BIN);
  Serial.print(b[2], BIN);
  Serial.println(b[3], BIN);
  //*/
}
  • Thank you for suggestion Martyans. You mentioned On Arduino pin 9 is Rx connect to Xbee Tx, pin 8 is Txconnect to Xbee Rx but in Arduino uno isn't pin 1 for Tx and 0 for Rx. Please excuse me if I am wrong. and one more thing I just want to send rpm from arduino to xbee and then xbee send rpm wirelessly to another xbee. Is it necessary to use Rx in Arduino? – zalt Oct 23 '14 at 5:55
  • Arduino uno has one Hardware Rx,Tx. I use it to communicate with PC(SerialMonitor). Programically i make pins 9,8 Rx,Tx two have second serial communication. Thats why i need library #include <SoftwareSerial.h> – Martynas Oct 23 '14 at 5:59
  • And about sending data. All the information is stored in byte shape, so it is easier to give Xbee byte and send to another Xbee. In your case, you give arduino "analog value". How he knows what value it is? How he can messure it correctly? There are to many risks to loose data and it is inaccurate. – Martynas Oct 23 '14 at 6:02
  • I got error message --> invalid operands of types 'float' and 'int' to binary operator for floatToByte[i] = (rpm >> 8*i); //Float to Byte – zalt Oct 23 '14 at 6:20
  • I got these arduino_to_xbee_serial.ino: In function 'void loop()': arduino_to_xbee_serial:45: error: invalid operands of types 'float' and 'int' to binary 'operator>>' arduino_to_xbee_serial:57: error: 'If' was not declared in this scope arduino_to_xbee_serial:57: error: expected `;' before '{' token – zalt Oct 23 '14 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.