Notifications
Clear all

Troubleshooting Raspberry Pi and Arduino communication.  

Page 3 / 4
  RSS

MadMisha
(@madmisha)
Eminent Member
Joined: 9 months ago
Posts: 44
2020-07-23 3:06 am  
Posted by: @teddy
Posted by: @madmisha

It should look something like this. Just print what comes over the serial. I did not test this.

 

#include <Wire.h>


void setup() {
// Join I2C bus as slave with address 8
Wire.begin(0x8);

// Call receiveEvent when data received
Wire.onReceive(receiveEvent);

}

// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.println(c);
}
}
void loop() {
delay(100);
}


In order to see what's going on in the Serial Monitor, you will need to add the following code in setup()

void setup() {

  Serial.begin(57600);

}

 

Note: You can change the speed to 9600.  Without that statement, nothing will come out of the Serial Port!

 

 

Yeah, that slipped my mind. Thanks


ReplyQuote
Bill21
(@bill21)
Active Member
Joined: 6 months ago
Posts: 16
2020-07-23 2:13 pm  
Posted by: @teddy

Rows and Columns line up with the HEX Code.

the rows and columns are not labeled. Please can you tell me what the rows signify and what the columns signify and how they line up?


ReplyQuote
Bill21
(@bill21)
Active Member
Joined: 6 months ago
Posts: 16
2020-07-23 2:37 pm  

@madmisha

the errors that I am getting in the Arduino IDE are:

If Funktion 'void receiveEvent(int)'

'serial' was not declared in this scope.

 

the code I am using is as follows

#include <Wire.h>

 

void setup() {
Wire.begin(0x8);

Wire.onReceive(receiveEvent);

Serial.begin(9600);

}

void receiveEvent(int howMany) {
while (Wire.available()) {
char c = Wire.read();
sereal.println(c);
}
}
void loop() {
delay(100);
}

 

I appreciate your help folks


ReplyQuote
MadMisha
(@madmisha)
Eminent Member
Joined: 9 months ago
Posts: 44
2020-07-23 10:53 pm  
Posted by: @bill21

@madmisha

the errors that I am getting in the Arduino IDE are:

If Funktion 'void receiveEvent(int)'

'serial' was not declared in this scope.

 

the code I am using is as follows

#include <Wire.h>

 

void setup() {
Wire.begin(0x8);

Wire.onReceive(receiveEvent);

Serial.begin(9600);

}

void receiveEvent(int howMany) {
while (Wire.available()) {
char c = Wire.read();
sereal.println(c);
}
}
void loop() {
delay(100);
}

 

I appreciate your help folks

Missed spelled line

Serial.println(c);

ReplyQuote
MadMisha
(@madmisha)
Eminent Member
Joined: 9 months ago
Posts: 44
2020-07-24 4:01 am  
Posted by: @bill21
Posted by: @teddy

Rows and Columns line up with the HEX Code.

the rows and columns are not labeled. Please can you tell me what the rows signify and what the columns signify and how they line up?

So here is an explanation. There are 2 dashes for each set because you are returning 2 digits. The dashes mean it did not get a response from that anything reporting to have the address. First 3 addresses are blank. Something about not being able to assign them, I'm not positive on that. If a cell is populated, that means it got a response from that address. (in your case it means that the Pi asked if there was anything on the line that was using that address and it responded. So your Pi was able to communicate and it was able to hear the response from the Arduino. Because it is assigned as 0x8, it should appear on the 00 row and under the 8 for column. When accounting for the format not lining up, yours did end up in that place.)

 

As you can see below, the row signifies what range it is. So of the 2 digit number it it starts with a 1 it will be in the 10 row. 7 will be in 70 row. The column is the last digit.

 

This is what it would look like if it was all filled with addresses. I am trying to format it correctly so hopefully this works.

   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a UU 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77

ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-24 4:14 am  
Posted by: @madmisha
Posted by: @bill21
Posted by: @teddy

Rows and Columns line up with the HEX Code.

the rows and columns are not labeled. Please can you tell me what the rows signify and what the columns signify and how they line up?

So here is an explanation. There are 2 dashes for each set because you are returning 2 digits. The dashes mean it did not get a response from that anything reporting to have the address. First 3 addresses are blank. Something about not being able to assign them, I'm not positive on that. If a cell is populated, that means it got a response from that address. (in your case it means that the Pi asked if there was anything on the line that was using that address and it responded. So your Pi was able to communicate and it was able to hear the response from the Arduino. Because it is assigned as 0x8, it should appear on the 00 row and under the 8 for column. When accounting for the format not lining up, yours did end up in that place.)

 

As you can see below, the row signifies what range it is. So of the 2 digit number it it starts with a 1 it will be in the 10 row. 7 will be in 70 row. The column is the last digit.

 

This is what it would look like if it was all filled with addresses. I am trying to format it correctly so hopefully this works.

   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a UU 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77

YES! That's correct!  I'm not sure what your trying to tell me. 

Everything will be just  --   Only the address of what you plug into i2c will show it's own HEX number.

 


ReplyQuote
Bill21
(@bill21)
Active Member
Joined: 6 months ago
Posts: 16
2020-07-25 6:23 pm  

@madmisha and teddy

That table is tremendous! It tells me that the I2C-1 bus is enabled and the the Pi and Arduino can see each other because the Arduino registers an address on the Pi. The wiring is correct and the physical wires are functional. I wonder if you would be so kind as to check my code for mistakes. I’ve triple checked it but I am severely dyslexic and can be blind to mistakes.

 

This is the python code I am using on the Pi. It compiles and runs as shown in Bill's Dronebot tutorial:

 

 

from smbus import SMBus

 

addr = 0x8 

bus = SMBus(1) 

 

numb = 1

 

print ("Enter 1 for ON or 0 for OFF")

while numb == 1:

    

    ledstate = input(">>>>    ")

 

 

if ledstate == "1":

    bus.write_byte(addr, 0x1)

elif ledstate == "0":

    bus.write_byte(addr, 0x0)

else:

    numb = 0

        

 

And this is the code I am using on the Arduino. It also compiles and uploads to the Arduino:

 

#include <Wire.h>

 

const int led = 13;

 

void setup() {

  Wire.begin(0x8);

  

  Wire.onReceive(receiveEvent);

  

 

  pinMode (led, OUTPUT);

  digitalWrite(led, LOW);

 

}

 

void receiveEvent(int howMany) {

  while (Wire.available()) {

    char c = Wire.read();

    digitalWrite(led, c);

      

  }

}

void loop() {

  delay (100);

}

 

In the Arduino code I have set the variable as led instead of ledPin because I am using the built in led on the Arduino to reduce the number of things that can go wrong. I’ve tested the led with the blink program. Whether I use led or ledPin in the program the physical led does not come on when I hit 1 and enter. If my code is correct, is it possible that, with all configurations disabled as a default, something else needs to be enabled in the Pi for the programs to work?

 

thanks again 

 

Bill21


ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-25 10:27 pm  

@bill21 - I found the error with the python code!      I'll attach it for you.

 

Once you download it, modify it's Rights:   chmod 755 misha.py

Now you can execute it with ./misha.py    rather then python misha.py

 

Happy coding!   Ted


ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-25 10:29 pm  

Sorry, forgot to attach the file

 


ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-26 1:30 am  
Posted by: @teddy

Sorry, forgot to attach the file

 

Misha & Bill  see attached files, 1 updated.   Also added clear screen on exit!

 

 


ReplyQuote
MadMisha
(@madmisha)
Eminent Member
Joined: 9 months ago
Posts: 44
2020-07-26 2:58 am  
Posted by: @teddy
Posted by: @teddy

Sorry, forgot to attach the file

 

Misha & Bill  see attached files, 1 updated.   Also added clear screen on exit!

 

 

It took me a really long time trying to spot the difference in your codes. I was really set on looking for a typo or incorrect procedure that I completely missed formatting! By the way, I don't need the code, just Bill.

 

@Bill21 So, in Python, spacing does matter. In Arduino we use mostly{} to signify what things belong to but in Python we use spaces(tabs). You can read it as if it came to that piece of code, if the next line is indented one space in from the command then it belongs to it. So in his code While and else are related that go on the same line(imagine a vertical line). So inside the While clause, ledstate is requested from input and on that same level and after it the If statement occurs. That means when you have the if statement you will need to indent what it is doing and the elif will need to be on the same imaginary vertical line as the IF.

 

Looking back, the last else is related to the IF statements and should actually be indented one. At-least in the way it was on the tutorial.


ReplyQuote
Bill21
(@bill21)
Active Member
Joined: 6 months ago
Posts: 16
2020-07-26 3:26 pm  

Thanks for taking the time and trouble folks. I've tried the variations in formatting and syntax. They all compile and run but the LED still doesn't come on. Once again the LED worked with the blink program. I've been doing other research. does anyone know if RPI.GPIO comes native with Geany in Raspbian? Or, what I can check to be sure I do have it installed?


ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-26 5:58 pm  
Posted by: @bill21

Thanks for taking the time and trouble folks. I've tried the variations in formatting and syntax. They all compile and run but the LED still doesn't come on. Once again the LED worked with the blink program. I've been doing other research. does anyone know if RPI.GPIO comes native with Geany in Raspbian? Or, what I can check to be sure I do have it installed?

I know nothing about Genry, but GPIO is installed with Buster for the Pi.

gpio -v will tell you if it's installed. gpio readall will show the state for each Pins.

Unfortunately, Gordon is NO LONG

supporting RPi.GPIO or wiringPi.  But with Buster, they are now built-in.

gpiozero has taken over and it needs LESS code then the above.

 

As for the Arduino, a call to 13 should lite the LED;  study the blink.ino

 

This post was modified 4 months ago 2 times by teddy

ReplyQuote
MadMisha
(@madmisha)
Eminent Member
Joined: 9 months ago
Posts: 44
2020-07-27 6:03 am  
Posted by: @bill21

Thanks for taking the time and trouble folks. I've tried the variations in formatting and syntax. They all compile and run but the LED still doesn't come on. Once again the LED worked with the blink program. I've been doing other research. does anyone know if RPI.GPIO comes native with Geany in Raspbian? Or, what I can check to be sure I do have it installed?

Geany is just the editor(IDE) on your computer to make the python file. You must upload the file(assuming you didn't create it directly on the Pi) to your Pi and then execute it. You will have to execute it in the terminal. Running it in Geany is only a test and won't do anything.

 

Wiring Pi is most likely pre-installed. It is on most distros for Pi.

 

Here is a simplified edit of Teddys python file.

 

It will just send a 0 and 1 on a small delay. You can mess around with the timing of the delay by changing the number next to the sleeps. The delay might not be long enough. There is no flag to force an exit in this version. So to end it in the terminal, hold ctrl and C.

 

You will have to upload it to the Pi.

 

You can also run that simplified arduino sketch to have it send to serial, just remember to include Serial.begin and set your baud rate.


ReplyQuote
teddy
(@teddy)
Trusted Member
Joined: 1 year ago
Posts: 63
2020-07-27 3:55 pm  
Posted by: @madmisha
Posted by: @bill21

Thanks for taking the time and trouble folks. I've tried the variations in formatting and syntax. They all compile and run but the LED still doesn't come on. Once again the LED worked with the blink program. I've been doing other research. does anyone know if RPI.GPIO comes native with Geany in Raspbian? Or, what I can check to be sure I do have it installed?

Geany is just the editor(IDE) on your computer to make the python file. You must upload the file(assuming you didn't create it directly on the Pi) to your Pi and then execute it. You will have to execute it in the terminal. Running it in Geany is only a test and won't do anything.

 

Wiring Pi is most likely pre-installed. It is on most distros for Pi.

 

Here is a simplified edit of Teddys python file.

 

It will just send a 0 and 1 on a small delay. You can mess around with the timing of the delay by changing the number next to the sleeps. The delay might not be long enough. There is no flag to force an exit in this version. So to end it in the terminal, hold ctrl and C.

 

You will have to upload it to the Pi.

 

You can also run that simplified arduino sketch to have it send to serial, just remember to include Serial.begin and set your baud rate.

Made a slight change to save some code,  Ted

 

 


ReplyQuote
Page 3 / 4