Tuesday, March 29, 2016

Raspberry Pi to Arduino Serial problems

After spending the entire weekend trying to communicate smoothly from my Tkinter program on my Raspberry Pi to the Arduino with the nanpy firmware loaded via serial, I almost gave up. Yet, I did not give up and at the end of the weekend I had a break through. After I did away with the nanpy firmware, thinking that was the issue, I wrote my own Arduino program. In doing this I ran into another problem in communication, adding to the first. The first problem I ran into was that the serial manager in the nanpy program was raising an error after the first command was issued. I couldn't understand why this was happening over and over again, so I removed the nanpy firmware and wrote a very simple serial communication program and loaded it on Arduino. In doing so, I ran into my second problem. Apparently, in python 2, the serial.write(variable) function automatically converts your string to bytes and sends them over serial. In python 3 this is not the case. You need to encode your string before placing it into the function. This ended up working: serial.write(bytes('1', 'UTF-8')). Once I correctly encoded the string, I returned to my first problem. Where the connection was failing after one operation. Now, I should mention what I was trying to accomplish. The Pi was telling the Arduino to turn a servo motor. Thinking back on this I feel pretty dumb. After noticing that the serial channel that the Arduino was connected was changing after every command I sent, I finally realized that powering the Arduino with the Pi was the obvious problem. Duh, ultimate learning moment. Powering a 5v Arduino from a 5v Pi and trying to turn a 6v motor was causing the Arduino to power down after every operation and power back up on a different serial channel. I had another power source set up and in place for use because I knew that I would need more power for my project but during testing purposes I had totally forgotten this. Maybe this will help people in the future who can't understand why their Arduino will not remain on the same channel.

No comments:

Post a Comment