Micropython Turnipbit Automatic Curtain Simulation System

Today, we will continue to study the function of the Turnipbit board. The inspiration for this experiment is not my idea. The office is shaded, and the sun cannot be seen at noon in the morning. Colleagues sitting opposite have to pull the curtains every day at four o'clock in the afternoon. , I can feel the sun shining directly on his eyes, (squinting). So thought of this.
Required components:
ü TurnipBit development board 1
ü Download data cable 1
ü Micro stepping motor (28BYJ-48) 1
ü Stepper motor driver board (ULN2003APG) 1
ü 1 photosensitive sensor
ü 1 TurnipBit expansion board
ü 1 computer connected to the network
ü Online visual programmer
http://turnipbit.com/PythonEditor/editor.html

Introduction of stepper motor

This experiment uses a 28BYJ-48 four-phase eight-beat motor with a voltage of DC5V~12V.
24BYJ48 The meaning of the name:
24: The outer diameter of the motor is 24mm B: The first letter of the pinyin of the step
word in the stepping motor
Y: The first letter of the pinyin of the permanent word in the permanent magnet Incoming motor is an open-loop control motor that converts electrical pulse signals into angular displacement 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 speed and stop position of the motor only depend on the frequency and number of pulses of the pulse signal, and are not affected by the load change. When the stepper driver receives a pulse signal, it drives the stepper motor to press The set direction is rotated by a fixed angle, called "step angle", and its rotation is performed step by step at a fixed angle. The angular displacement can be controlled by controlling the number of pulses, so as 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, so as to achieve the purpose of speed regulation.


Driving principle
When the control pulse is continuously sent to the motor, the motor will rotate continuously. Each pulse signal corresponds to a change of the energization state of a certain phase or two-phase winding of the stepping motor once, and the corresponding rotor will rotate through a certain angle (step angle). When the change of energization state completes a cycle, the rotor rotates through one pitch.
Four-phase stepper motors 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…

device connection

1. Insert the white connector of the stepper motor into the corresponding seat of the driver board.
2. Insert the TurnipBit into the expansion board correspondingly, remember that the side with the LED lights and buttons 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 ULN2003APG driver board
P5 - IN1
P8 - IN2
P11 - IN3
P12 - IN4
+5V - 5V positive

GND - negative

4. Connect the photosensitive sensor to the TurnipBit expansion board. The wiring diagram is as follows:
TurnipBit expansion board - photosensitive sensor
3.3V VCC
GND GND
P0 AO

start programming

The previous tutorials have always used drag-and-drop visual programming. This method can quickly get started with zero-based partners, 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, this spelling programming is too troublesome)

Friends who have seen programming before, it is not difficult to find that the first thing you enter when you open the editor is the code programmer interface.

5.4.2. Next, use code to control the stepper motor and collect illumination data.
ü When the light becomes weak, the stepper motor rotates clockwise once to simulate the operation of closing the curtain;
ü When the light becomes stronger, the stepper motor rotates counterclockwise once to simulate the operation of opening the curtain;
complete the source code:

# 在这里添加Python代码
from microbit import *

Pin_All=[pin5,pin8,pin11,pin12]

#转速(ms) 数值越大转速越慢 最小值1.8ms
speed=5

STEPER_ROUND=512 #转动一圈(360度)的周期
ANGLE_PER_ROUND=STEPER_ROUND/360 #转动1度的周期

isOpen=False#表示窗帘的状态 True:打开 False:关闭

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()#读取光敏传感器传递过来的模拟量数据
    #数值越大说明光照度越小
    if light>400:
        if isOpen:
            isOpen=False
            SteperRun(-360)#光照小于设定的值,关闭窗帘
    else:
        if isOpen==False:
            isOpen=True
            SteperRun(360)#光照大于设定的值,打开窗帘

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517881&siteId=291194637