[TPYBoard, tpyboard schematic
Reprint Please note: @ small Wuyi http://www.cnblogs.com/xiaowuyi
Welcome to the discussion group 64770604 I. Experimental Equipment 1. A TPYboard V102 Board 2. A motor drive module L298N 3. A motor two blocks 4. A car chassis 5. An Ultrasonic Module 6. A 5110 Screen
Ii. Ultrasonic Module 1. What is an ultrasonic module?
An ultrasonic sensor is a sensor developed based on the characteristics of ultrasound. It transmits an ultrasonic wave (far higher than the Human Auditory range) and provide an output pulse corresponding to the time the burst echo returns to the sensor. Ultrasonic Sensors are widely used in non-contact measurement, such as detecting liquid levels (especially corrosive liquids, such as sulfuric acid and nitric acid liquids), automotive reversing anti-collision systems, metal/non-metallic flaw detection, etc, ultrasonic Distance sensors can be used.
2. Principles of ultrasonic module ranging
(1) TRIG remote sensing with I/O port is used to deliver a high-voltage signal of at least 10 us.
(2) The module automatically sends 8 40 khz square waves to detect whether any signal is returned.
(3) When a signal is returned, a high level is output through the ECHO of the IO port. The duration of the high level is the time from the time when the ultrasonic wave is sent to the return time. Test distance = (high level time * Sound speed (340 M/S)/2.
Iii. Experiment 1: ultrasonic module ranging with 5110 for display 1. Connection Method
(1) ultrasonic module connection method
VCC provides 5 V power supply, GND is the ground line, TRIG trigger control signal input, X2 of the board, ECHO Signal output, X1 of the board, four interface ends.
(2) 5110 connection and usage
See specific: http://www.cnblogs.com/xiaowuyi/p/6347336.html
2. Original code
Import the font. py file and the upcd8544.py file, and write main. py
The main. py code is as follows:
import pybfrom pyb import Pinfrom pyb import Timerimport upcd8544from machine import SPI,PinTrig = Pin('X2',Pin.OUT_PP)Echo = Pin('X1',Pin.IN)num=0flag=0run=1def start(t): global flag global num if(flag==0): num=0 else: num=num+1def stop(t): global run if(run==0): run=1start1=Timer(1,freq=10000,callback=start)stop1=Timer(4,freq=2,callback=stop)while True: if(run==1): SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in) #CLK =>SPI(1).SCK 'X6' SPI clock RST = pyb.Pin('Y10') CE = pyb.Pin('Y11') DC = pyb.Pin('Y9') LIGHT = pyb.Pin('Y12') lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT) Trig.value(1) pyb.udelay(100) Trig.value(0) while(Echo.value()==0): Trig.value(1) pyb.udelay(100) Trig.value(0) flag=0 if(Echo.value()==1): flag=1 while(Echo.value()==1): flag=1 if(num!=0): #print('num:',num) distance=num/10000*34299/2 print('Distance:') print(distance,'cm') lcd_5110.lcd_write_string('Distance',0,0) lcd_5110.lcd_write_string(str(distance),0,1) lcd_5110.lcd_write_string('cm',50,1) flag=0 run=0
3. Effect
(1) Approaching obstacles
(2) Stay away from obstacles
4. Experiment 2: obstacle avoidance Vehicle 1. What is a motor drive module?
The motor drive module can be used to control the operation of the motor: speed control, operation, stop, stepping, and constant speed.
2. L298N connection and usage
The L298N module is a two-way H-bridge drive, so two motors can be driven at the same time. After the method enables ena enb, the PWM signal can be input from IN1 IN2 to drive the speed and direction of motor 1, respectively, you can input a PWM signal from IN3 IN4 to drive the speed and direction of motor 2. We connect the OUT1 and OUT2 of the motor 1 interface with the positive and negative ends of one motor of the trolley, and connect OUT3 of the motor 2 interface with the positive and negative ends of another motor of the trolley. Connect the terminals on both sides, I .e., the power supply positive pole (the middle terminal is grounded) to VIN OF THE TPYboard, and the middle terminal is grounded to connect GND OF THE TPYBoard, in1-In4 connects TPYBoard Y1, Y2, Y3, Y4, through Y1, Y2 and Y3, Y4 level, to control the motor rotation, so that the car forward, backward, to the left, right.
3. Use the chassis + V102 + ultrasonic + L298N. When the car moves forward, the car turns itself to avoid obstacles.
(1) original code main, py file
# main.py -- put your code here!import pybfrom pyb import Pinfrom pyb import Timerdef start(t): global flag global num if(flag==0): num=0 else: num=num+1def stop(t): global run if(run==0): run=1def left(): x1.high() x2.low() y1.high() y2.low()def go(): x1.high() x2.low() y1.low() y2.high()def back(): x1.low() x2.high() y1.high() y2.low()def right(): x1.low() x2.high() y1.low() y2.high()def stop(): x1.low() x2.low() y1.low() y2.low()Trig = Pin('X9',Pin.OUT_PP)Echo = Pin('X10',Pin.IN)num=0flag=0run=1start1=Timer(1,freq=10000,callback=start)stop1=Timer(4,freq=2,callback=stop)x1 = Pin('X1', Pin.OUT_PP)x2 = Pin('X2', Pin.OUT_PP)y1 = Pin('Y1', Pin.OUT_PP)y2 = Pin('Y2', Pin.OUT_PP)while True: if(run==1): Trig.value(1) pyb.udelay(100) Trig.value(0) while(Echo.value()==0): Trig.value(1) pyb.udelay(100) Trig.value(0) flag=0 if(Echo.value()==1): flag=1 while(Echo.value()==1): flag=1 if(num!=0): #print('num:',num) distance=num/10000*34299/2 print('Distance') print(distance,'cm') if distance>=20: go() if distance<=20: stop() back() flag=0 run=0
(2) Effects
If the above video cannot be viewed, visit the http://admin.turnipsmart.com/IMG_3362.mp4