Hi, this instructable is part of a bigger project where I use a keyboard connected to a raspberry pi to control an arduino that is doing the hard work of controlling some motors and sensors. Later on I might update this instructable in order to use the SPI or I2C pins of the raspberry pi, right now I´m using 3 pins to control the arduino without feedback.
For this project im using the WiringPi library, available here.
Also, all the code is written in C/C++, but you can use the WiringPi with python, see the link for more details
Attention: When connecting the arduino and the raspberry pi remember that the GPIOs of the pi work at 3.3 volts and the arduino works at 5v but since 3.3 V is considered the HIGH logic level by the arduino it is ok to connect without any voltage converter. However, if you want to send data from the arduino to the raspberry you´ll need a voltage divider.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Installing WiringPi
Wiring pi is a good library to use if you´re familiar with arduino language.
You can find the instructions on how to install WiringPi in the developers website.
Just be aware that without a voltage divider you can only use it in one way (raspberry pi->arduino).
make sure you run the blink example in order to know if everything is working ok.
Step 2: Getting Info From the Keyboard and Sending It to the IO Pins
To get information I wrote this piece of code.
It lets me use the WASD keys to control a robot, if you used a standard scanf() its always waiting for the enter key in order to move out of the command, with this code you dont need to always push the enter key after the key you want to press.
I found some code about this and copied it and tweaked it to my application, don´t ask me who made or where to find the original because it was some time ago and it didnt´t had a name attached to it.
with this code you just need to know the name of the variable where the key you want pressed. For normal letters is just KEY_thenameoftheletter (example: KEY_X,KEY_S, etc).
For my application I used the UART port of the raspberry pi to talk with the UART port of and arduino that got the info of a number of sensors and controlled the motors.
To control the UART pins I used the WiringSerial library from the wiringPi repository, to open the connection you need to use the serialOpen function. this function receives the location of the port (in the case of the raspberry pi is in the dev/ttyAMA0) and the baudrate of the connection (I needed a fast connection and the distance between the rasp and the arduino was short so I went with 115200 of baudrate).
To send a character you need to use the serialPutchar function, where you give the function an integer (returned by the serialOpen function) and the byte you want to send( a character).
Step 3: Step 3: Physical Interface
Like I said in Step 1, you need to watch the voltage since the arduino uses the 5v logic level and the raspberry uses the 3.3v logic level. although the arduino is 3.3v sensitive, the rasp isnt 5v sensitive and if you connect the two ports without any interface the most likely outcome is that you fry yours gpio rasp pins.
To prevent against this there are two options:
- Making a voltage divider: its simple (uses two resistors) and can be made quickly
- Using a 3.3v & 5v logic converter: you can get a board like this from sparkfun but they released the eagle files so you can make your own converter if you have the materials and tools to make it (pdf schematic)
Sparkfun also has a good hookup about this breakout board that you can find here.
Its possible to find which pins are the uart port in the picture above (if you´re rasp isn´t a rasp2 you might want to find in google a pinout image in since it might not be the same pinout.
Step 4: Step 4: Conclusion
This is my first instructable so have a little mercy :D.~
Any question you have ask away, I´m more than happy to help :)