Micropython Turnipbit automatic curtain simulation system
[Copy link]
Today we continue to study the functions of the Turnipbit board. The inspiration for this experiment was not mine. The office is in the shade, and the sun cannot be seen in the morning or at noon. The colleague sitting opposite me has to draw the curtains at 4 pm every day, and I can feel the sun shining directly into his eyes (squinting). So I came up with this idea. Required components: ü 1 TurnipBit development board ü 1 download data cable ü 1 micro stepper motor (28BYJ-48) 1 piece ü Stepper motor driver board (ULN2003APG) 1 piece ü Photosensitive sensor 1 piece ü TurnipBit expansion board 1 piece ü 1 computer connected to the Internet ü Online visual programmer http://turnipbit.com/PythonEditor/editor.html Introduction to stepper motor The experiment uses 28BYJ-48 four-phase eight-beat motor with a voltage of DC5V~12V. 24BYJ48The meaning of the name: 24: Motor outer diameter 24mm B: The first pinyin letter of the word "bu" in stepper motor [align=left ]Y: The first pinyin letter of the word "永" in permanent magnetJ: The first pinyin letter of the word "减" in deceleration 48: Four-phase 8-step Stepper motor is an open-loop controlled motor that converts electrical pulse signals into angular or linear displacement. It is the main executive element in modern digital program control systems and is widely used. In the case of non-overload, the motor speed and stop position only depend on the frequency and number of pulses of the pulse signal, and are not affected by load changes. When the stepper driver receives a pulse signal, it drives the stepper motor to rotate a fixed angle in the set direction, called the "step angle". Its rotation runs step by step at a fixed angle. The angular displacement can be controlled by controlling the number of pulses to achieve the purpose of accurate positioning; at the same time, the speed and acceleration of the motor rotation can be controlled by controlling the pulse frequency to achieve the purpose of speed regulation. Driving principle When control pulses are sent to the motor continuously, the motor will rotate continuously. Each pulse signal corresponds to a change in the power-on state of one or two phases of the stepper motor winding, and the corresponding rotor will rotate a certain angle (step angle). When the change in the power-on state completes a cycle, the rotor rotates one tooth pitch. The four-phase stepper motor can operate in different power-on modes. Common power-on modes: Four-beat (single-phase winding power-on): ABCDA… Double four-beat (two-phase winding power-on): AB-BC-CD-DA-AB-… Eight beats: A-AB-B-BC-C-CD-D-DA-A… Connection of devices 1. Plug the white connector of the stepper motor into the corresponding socket of the driver board. 2. Insert the TurnipBit into the expansion board, remember that the side with the LED light and button faces the side with the pins of the expansion board. 3. Connect the driver board to the TurnipBit expansion board. The wiring diagram is as follows: TurnipBit expansion board | | | | | | | | | | | | | |
4. Connect the light sensor to the TurnipBit expansion board. The wiring diagram is as follows: TurnipBit expansion board | | | | | [align= left] GND | | [align= left] AO |
[/ align] Start programming[/ align] Previous tutorials have always used drag-and-drop visual programming. This method can quickly teach beginners with zero foundation. It is easy to get started, but in actual projects or work, you still have to type code directly. Today I will introduce another function of TurnipBit's visual programmer - code programming. (Actually, the programming of this puzzle is too troublesome ) [backcolor =transparent]For those who have watched programming before, it is not difficult to find that the first thing you enter when you open the editor is the code programmer interface. [/ size] 5.4.2, Next, use code to control the stepper motor and collect light data. [ size=18px]ü When the light becomes weak, the stepper motor rotates clockwise for one circle to simulate the operation of closing the curtains; ü When the light becomes stronger, the stepper motor rotates counterclockwise for one circle to simulate the operation of opening the curtains; Complete source Code: - # Add Python code herefrom microbit import * Pin_All=[pin5,pin8,pin11,pin12] #Speed (ms) The larger the value, the slower the speed. The minimum value is 1.8ms speed =5 STEPER_ROUND=512 #The period of one circle (360 degrees) ANGLE_PER_ROUND=STEPER_ROUND/360 #The period of 1 degree isOpen=False#Indicates the state of the curtain True: open False: closed def SteperWriteData(data): count=0 for i in data: Pin_All[count].write_digital(i) count+=1 def SteperFrontTurn(): global speed SteperWriteData([1,1,0,0]) sleep(speed) SteperWriteData([0,1,1,0] ) sleep(speed) SteperWriteData([0,0,1,1]) sleep(speed) SteperWriteData([1,0,0,1]) sleep(speed) def SteperBackTurn(): global speed SteperWriteData([1,1,0,0]) sleep(speed) SteperWriteData([1,0,0,1] ) sleep(speed) SteperWriteData([0,0,1,1]) sleep(speed) SteperWriteData([0,1,1,0]) sleep(speed) def SteperStop(): SteperWriteData([0,0,0,0]) def SteperRun(angle): global ANGLE_PER_ROUND val=ANGLE_PER_ROUND*abs(angle) if(angle>0): for i in range(0,val): SteperFrontTurn() else: for i in range(0,val): SteperBackTurn() SteperStop() while True: light=pin0.read_analog()#Read the analog data transmitted by the photosensor#The larger the value, the lower the light intensityif light>400: if isOpen: isOpen=False SteperRun(-360)#The light is less than the set value, close the curtainselse: if isOpen==False: isOpen=True SteperRun(360)#The light is greater than the set value, open the curtains
复制代码 |
This content was created by loktar, a user of EEWORLD forum. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source
|