Metainformationen zur Seite
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
computer:raspberrypi:mit_arduino_koppeln [2013/07/22 20:48] ristigl angelegt |
computer:raspberrypi:mit_arduino_koppeln [2018/03/16 21:11] (aktuell) |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== RaspberryPi mit Arduino koppeln ====== | ====== RaspberryPi mit Arduino koppeln ====== | ||
+ | Um den Arduino mit dem Raspberry Pi zu verbinden muss man lediglich die TX-Leitung des Pis mit der RX-Leitung des Arduinos und jeweils die Masseleitung miteinander verbinden. | ||
+ | {{ :computer:raspberrypi:serielle_kommunikation_steckplatine.png |}} | ||
+ | |||
+ | ===== Arduino ===== | ||
+ | Auf der Arduino-Seite reicht folgendes kleines Programm aus, um Daten zu empfangen, die an der seriellen Schnittstelle ankommen (dabei darf die Datenleitung während des Programmierens **nicht** mit dem Pi verbunden sein!): | ||
+ | |||
+ | <code cpp> | ||
+ | int i = 0; // for incoming serial data | ||
+ | void setup() { | ||
+ | Serial.begin(9600); // opens serial port, sets data rate to 9600 bps | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if (Serial.available() > 0) { // send data only when you receive data: | ||
+ | i = Serial.read(); // read the incoming byte: | ||
+ | Serial.println(i, DEC); // say what you got: | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ===== Raspberry Pi ===== | ||
+ | Zuerst muss man die serielle Schnittstelle für die eigene Nutzung freischaufeln (zuvor diente die Schnittstelle, um dort Terminals zu betreiben.) | ||
+ | <code> | ||
+ | sudo nano /etc/inittab | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | 1:2345:respawn:/sbin/getty 115200 tty1 | ||
+ | # Line below commented out | ||
+ | # 2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 | ||
+ | 3:23:respawn:/sbin/getty 115200 tty3 | ||
+ | 4:23:respawn:/sbin/getty 115200 tty4 | ||
+ | 5:23:respawn:/sbin/getty 115200 tty5 | ||
+ | 6:23:respawn:/sbin/getty 115200 tty6 | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | sudo nano /boot/cmdline.txt | ||
+ | </code> | ||
+ | folgende Zeilen auskommentieren: | ||
+ | <code> | ||
+ | ... | ||
+ | console=ttyAMA0,115200 | ||
+ | ... | ||
+ | kgdboc=ttyAMA0,115200 | ||
+ | ... | ||
+ | </code> | ||
+ | |||
+ | Für das Programm braucht man noch Python-Bibliotheken, die man schnell installiert: | ||
+ | <code> | ||
+ | apt-get install python-pip | ||
+ | pip install pyserial | ||
+ | </code> | ||
+ | |||
+ | Anschließend den Raspberry Pi neu starten: | ||
+ | <code> | ||
+ | sudo reboot | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Das nachfolgende Programm übergibt abwechselnd 0 und 255 an den Arduino: | ||
+ | <code python> | ||
+ | import serial | ||
+ | import time | ||
+ | |||
+ | delay = 0.5 | ||
+ | comunicacion = serial.Serial('/dev/ttyAMA0',9600) | ||
+ | comunicacion.close() | ||
+ | comunicacion.open() | ||
+ | |||
+ | while True: | ||
+ | comunicacion.write(chr(0)) | ||
+ | time.sleep(delay) | ||
+ | comunicacion.write(chr(255)) | ||
+ | time.sleep(delay) | ||
+ | </code> | ||