Benjy
Benjy

Reputation: 115

Raspberry pi receives numbers from arduino

I am experimenting with connecting Raspberry Pi to Arduino, and when I set a loop to receive info from Arduino Uno serial, it only receives:

'118537\r\n'

That is when I try to serial print 'Hi'

Here is my arduino code:

void setup(){
  Serial.begin(9600);
}
void loop(){
  Serial.println('Hi');
  delay(2000);
}

Here is my python 3.2 code:

import serial
ser = serial.Serial('/dev/ttyACM0')
while True:
    print(ser.readline())

This prints: '118537\r\n' every 2 seconds.

How can I get the original 'Hi' every 2 seconds?

Upvotes: 0

Views: 86

Answers (1)

Benjy
Benjy

Reputation: 115

For those of you who want to know, it was the fact that I used ' instead of " in the string, changing:

Serial.println('Hi');

to

Serial.println("Hi");

eta: the reason why '118537\n\n' is printed out is because 'Hi' is a literal array of two bytes rather than a "string" and therefore the compiler uses the function for printing an int. In fact, the hex code of H is 48 and the hex code of i is 69, and the hexadecimal value of 0x4869 is exactly 118537 in decimal notation.

Upvotes: 3

Related Questions