[Tpyboard-micropython python can do hardware 8] Learn to use Bluetooth modules and servos

Source: Internet
Author: User

Reprint please specify: @ Xiao Wu Yi Http://www.cnblogs.com/xiaowuyi

Welcome to join the discussion group64770604First, the experimental equipment 1, tpyboard V102 Board  piece2. Motor Drive Modulel298na3. Motor two Bucks .4, car chassis5, ultrasonic module6,5110screen Piece

Second, ultrasonic module 1, what is the ultrasonic module

The ultrasonic sensor is a sensor developed using the characteristics of ultrasound , which works by transmitting an ultrasonic (much higher than the human hearing range) and providing an output pulse corresponding to the time required for the burst echo to return to the sensor. Ultrasonic sensors in the non-contact measurement of a wide range of applications, such as the detection of liquid water level (especially corrosive liquids, such as sulfuric acid, nitric acid liquid), car reversing collision avoidance system, metal / non-metallic flaw detection, etc., can be used to ultrasonic distance sensor.

2. The principle of ultrasonic module ranging

(1) The use of IO port trig trigger ranging, to a minimum of 10us high-level ordinary mail presented.

(2) The module automatically sends 8 square waves of 40khz, and automatically detects if a signal is returned.

(3) There is a signal return, through the IO port ECHO output A high level, the duration of the high level is the ultrasonic wave from the launch to the time of return. Test distance = (high-normal * sound velocity (340m/s))/2.

Three, experimental one: Ultrasonic module ranging, with 5110来 display 1, connection method

(1) Ultrasonic Module connection method

VCC for 5V power supply, GND for ground, TRIG trigger control signal input, the board's X2,echo echo signal output, the board X1, four interface end.

(2) 5110 wiring and how to use

See details: http://www.cnblogs.com/xiaowuyi/p/6347336.html

2, the original code

Import font.py files and upcd8544.py files, write main.py

The code for main.py is as follows:

Import pybfrom pyb import pinfrom pyb import timerimport upcd8544from machine Import Spi,pintrig = Pin (' X2 ', pin.out_pp) Ech        o = 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,FR Eq=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 (+) trig.value (0) while (Echo.value ( ) ==0): Trig.value (1) pyb.udelay (+) 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_writ E_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) Close to obstacles

(2) Stay away from obstacles

Four, experiment two: Obstacle avoidance trolley 1, what is the motor drive module

Motor Drive module is mainly able to control the operation of the motor: speed, operation, stop, step, uniform and other operations

2, l298nHow to connect and use

The l298n module is2of the roadHcan drive up to two motors at the same time, enablingENA ENBafter that, you can separate fromIN1 IN2inputPWMSignal Driven Motor1 speed and direction, can be separated fromIN3 IN4inputPWMSignal Driven Motor2the speed and direction of。 We will motor1interface ofOUT1with theOUT2connected to the positive and negative poles of a motor of a car, the motor2interface ofOUT3with theOUT2connected to the positive and negative poles of the other motor of the car. Then connect the terminals on both sides, i.e. the power supply positive (the middle terminal is grounded)Tpyboardof theVIN, the middle terminal is grounded and connectedTpyboardof theGND,In1-in4ConnectionTpyboardof theY1,Y2,Y3,Y4, throughY1,y2with theY3,y4High and low levels to control the rotation of the motor, so that the car forward, back, left, to the right.

3. Use the chassis+v102+Ultrasonic+l298nwhen the car is moving forward, the obstacle will turn itself to avoid

(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 N     Um 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=t Imer (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 ( Trig.value (0) while (Echo.value () ==0): Trig.value (1) pyb.udelay (+) Tr Ig.value (0) flag=0       if (Echo.value () ==1): Flag=1 while (Echo.value () ==1): Flag=1 i F (num!=0): #print (' num: ', num) DISTANCE=NUM/10000*34299/2 print (' Distance ') pri                NT (distance, ' cm ') if Distance>=20:go () if Distance<=20:stop () Back () flag=0 run=0

(2) Achieve effect

If the above video cannot be browsed, please visit http://admin.turnipsmart.com/IMG_3362.mp4

[Tpyboard-micropython python can do hardware 8] Learn to use Bluetooth modules and servos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.