Raspberry Pi controle externe speelgoedauto
Hardware:
1. Raspberry Pi B + of hoger
2. (4) 2N3904 NPN (EBC) V6
3. (8) 10K Resisters
4. (4) 1K Resisters
5 (4) LED Light (elke kleur)
How to Make?
Zie hierboven foto 's
PIN verbinding
Raspberry Pi--Controle Circuit--afstandsbediening
GPIO 17--Circuit A--links
GPIO 27--Circuit B--rechts
GPIO 22--Circuit C--vooruit
GPIO 23--Circuit D--achteruit
Simple Python Test
Deze eenvoudige test is het bepalen van de GPIO 4 ON en OFF, je moet verbinden met voorwaarts Pin op speelgoed auto Remote voordat RUN
$ sudo nano simpletest.py
<pre><p><strong>import</strong> time</p><p><strong>import</strong> RPi.GPIO <strong>as</strong> io</p><p>io.setmode(io.BOARD)</p><p>pir_pin = 7</p><p>io.setup(pir_pin, io.OUT)</p><p><strong>while</strong> True:</p><p> io.output(pir_pin, io.HIGH)</p><p> <strong>print</strong>(<strong>"Forward On"</strong>)</p><p> time.sleep(3)</p><p> io.output(pir_pin, io.LOW)</p><p> <strong>print</strong>(<strong>"Forward OFF"</strong>)</p><p> time.sleep(3)</p>
Volledig Script
$sudo nano toycarremote.py
<p>import time <br>import RPi.GPIO as gpio </p><p>class ToyCarRemote: def __init__(self, forward_pin=11, backward_pin=13, left_pin=15, right_pin=16, test_pin=7): #gpio.setmode(gpio.BCM) #test_pin = 4 # GPIO 4 #forward_pin = 17 # (GPIO 17) #backward_pin = 27 # GPIO 27 #left_pin = 22 # GPIO 22 #right_pin = 23 # GPIO 24</p><p> self.test_pin=test_pin self.forward_pin=forward_pin self.backward_pin=backward_pin self.left_pin=left_pin self.right_pin=right_pin</p><p> gpio.setmode(gpio.BOARD) test_pin = 7 # GPIO 4 forward_pin = 11 # (GPIO 17) backward_pin = 13 # GPIO 27 left_pin = 15 # GPIO 22 right_pin = 16 # GPIO 23</p><p> gpio.setup(self.test_pin, gpio.OUT) gpio.setup(self.forward_pin, gpio.OUT) gpio.setup(self.backward_pin, gpio.OUT) gpio.setup(self.left_pin, gpio.OUT) gpio.setup(self.right_pin, gpio.OUT)</p><p> gpio.output(self.forward_pin, gpio.LOW) gpio.output(self.backward_pin, gpio.LOW) gpio.output(self.left_pin, gpio.LOW) gpio.output(self.right_pin, gpio.LOW) </p><p> def clearAll(self): gpio.output(self.forward_pin, gpio.LOW) gpio.output(self.backward_pin, gpio.LOW) gpio.output(self.left_pin, gpio.LOW) gpio.output(self.right_pin, gpio.LOW) </p><p> def updown(self, direction, OnOff, tsecond): # direction F:forward , B:Backward if direction in ('F'): gpio.output(self.backward_pin, gpio.LOW) gpio.output(self.forward_pin, OnOff) if direction in ('B'): gpio.output(self.forward_pin, gpio.LOW) gpio.output(self.backward_pin, OnOff) time.sleep(tsecond)</p><p> def leftright(self, direction, OnOff, tsecond): # direction L:left , R:Right if direction in ('L'): gpio.output(self.right_pin, gpio.LOW) gpio.output(self.left_pin, OnOff) if direction in ('R'): gpio.output(self.left_pin, gpio.LOW) gpio.output(self.right_pin, OnOff) time.sleep(tsecond)</p><p>#try: # while True: # gpio.output(test_pin, gpio.HIGH) # #except KeyboardInterrupt: # gpio.cleanup()</p><p>if __name__ == '__main__': tcr = ToyCarRemote() for i in range(20): print('\n', 'Loop : ', i) print 'Forward 2 Second' tcr.updown('F', gpio.HIGH , 0) tcr.leftright('L', gpio.HIGH, 0) time.sleep(2) tcr.clearAll() print 'Stop Forward' tcr.updown('B', gpio.HIGH, 0) time.sleep(2) tcr.clearAll()</p>
Meer informatie: iMediaBank - http://www.imediabank.com