Code a stepper motor
- [Presenter] Last week we connected a stepper motor to a Raspberry Pi. We used a motor driver, the L293D to handle the current that's too large for the Raspberry Pi. This week let's spend some time with the code controlling the hardware. Hi, I'm Mark Niemann-Ross and welcome to this week's edition of Raspberry Pi Weekly. Every week we explore the Raspberry Pi and share useful tips. Let's start with the project we built last week. This connects a stepper motor to the Raspberry Pi. The code to control this is found in the exercise files. Chapter two. Zero two, zero eight step motor dot Pi. On the Raspberry Pi, you can use the Thorny application to open up code. Lines one through 13 set up definitions we'll need later in the code. Lines 16 and 17 set up four GPIO pins to be outputs. Lines 20 and 21 enable the L293D chip so it can turn on the motor controller circuits. Lines 25 through 27 sets up logic that will control the motors. Look at line 26, turn clockwise is defined as a list of tuples. Zero comma one comma zero comma zero. Then zero comma one comma zero comma one, et cetera. Each tuple contains a combination of ones and zeros. The ones and zeros represent the on/off state of the four enabled pins on the L293D motor controller. Each tuple represents a step in the sequence to turn the stepper motor. Run the sequence one way, the motor turns forward. Run the sequence backwards, and the motor turns backwards. And that's exactly what the function defined in line 29 does. The turn the motor function controls the number of steps which controls how far the motor turns. It also controls the delay between steps which controls how fast the motor turns, and it controls the clockwise or counterclockwise direction of the motor. Line 35 places a value in delay variable which will control the speed of the rotation. Line 36 defines the number of steps of rotation. This controls how far around the motor turns. Finally, in line 38 a while loop starts that sends a command to turn clockwise then counterclockwise and then repeat ten times. I encourage you to experiment with this code, in particular lines 26, 35 and 36. Or can you read a value from an input and then reflect that value in the direction and speed of the stepper motor. For example, try creating an analog clock by reading the time and then moving the motor to point to the current hour. Thanks for joining me for this episode of Raspberry Pi Weekly. Be sure to join the LinkedIn group and check out previous episodes on LinkedIn Learning. I'll see you next week with more Raspberry Pi adventures.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
The Raspberry Pi family3 分钟 53 秒
-
(已锁定)
GPIO male to female jumpers3 分钟 30 秒
-
(已锁定)
Weatherproofing Pi4 分钟 19 秒
-
(已锁定)
Power and batteries3 分钟 57 秒
-
(已锁定)
IR rangefinder project2 分钟 26 秒
-
(已锁定)
Clock project6 分钟 20 秒
-
(已锁定)
Wire a stepper motor3 分钟 38 秒
-
Code a stepper motor3 分钟 37 秒
-
(已锁定)
gpiozero library4 分钟 50 秒
-
(已锁定)
Take a screenshot4 分钟 26 秒
-
(已锁定)
Analog to digital2 分钟 38 秒
-
(已锁定)
Autostart file setup4 分钟 24 秒
-
(已锁定)
Simple connection to a speaker2 分钟 43 秒
-
(已锁定)
Red LED, RGB LED, blink, and glow3 分钟 23 秒
-
Connect input to output: Range finder to LED2 分钟 4 秒
-
(已锁定)
Raspberry Pi Desktop on a laptop4 分钟 10 秒
-
(已锁定)
Programming with Node-RED6 分钟 15 秒
-
(已锁定)
Programming IoT with IFTTT5 分钟 24 秒
-
(已锁定)
Update the Raspberry Pi with apt-get3 分钟 3 秒
-
(已锁定)
A clean and simple web server5 分钟 23 秒
-
(已锁定)
Use SSH to control the Raspberry Pi3 分钟 28 秒
-
(已锁定)
Camera control with raspistill4 分钟
-
(已锁定)
GPIO pinout charts3 分钟 53 秒
-
(已锁定)
Back up the Raspberry Pi3 分钟 1 秒
-
(已锁定)
Use SmartSim to explore logic circuits4 分钟 47 秒
-
(已锁定)
Raspberry Pi 46 分钟 29 秒
-
(已锁定)
Change your default password2 分钟 44 秒
-
(已锁定)
Mathematica and general-purpose input/output (GPIO)6 分钟 7 秒
-
(已锁定)
Build a Raspberry Pi private cloud8 分钟 40 秒
-
Dealing with undervoltage3 分钟 50 秒
-
(已锁定)
Play music with Sonic Pi5 分钟 53 秒
-
(已锁定)
Add a printer to Raspberry Pi3 分钟 38 秒
-
(已锁定)
Use Screenly for web kiosks5 分钟 59 秒
-
(已锁定)
Use raspivid for Raspberry Pi Videos4 分钟 55 秒
-
(已锁定)
Build an MQTT publisher6 分钟 33 秒
-
(已锁定)
Build an MQTT client4 分钟 5 秒
-
(已锁定)
Connect to MQTT with Node-RED9 分钟 55 秒
-
(已锁定)
Build an MQTT broker4 分钟 44 秒
-
(已锁定)
A power switch for the Raspberry Pi3 分钟 57 秒
-
(已锁定)
Input polling vs. interrupts5 分钟 41 秒
-
(已锁定)
GPIO Pi headers2 分钟 34 秒
-
(已锁定)
High-grade audio for the Raspberry Pi4 分钟 34 秒
-
(已锁定)
Program the Sense HAT with Node-RED6 分钟 54 秒
-
(已锁定)
Remote GPIO6 分钟 5 秒
-
(已锁定)
Framboisedorf4 分钟 6 秒
-
(已锁定)
Use Kano OS5 分钟 15 秒
-
(已锁定)
What's new with Scratch 3.0?2 分钟 57 秒
-
(已锁定)
Controlling high voltages with the Raspberry Pi4 分钟 53 秒
-
(已锁定)
Rotary phone: Intro and switches3 分钟 12 秒
-
(已锁定)
Rotary phone: Ringer2 分钟 44 秒
-
(已锁定)
Rotary phone: Speaker5 分钟 28 秒
-
(已锁定)
Piscope5 分钟 33 秒
-
Cron on the RPI7 分钟 45 秒
-
(已锁定)
Keep your plants watered with a Raspberry Pi4 分钟 42 秒
-
(已锁定)
Grove HAT4 分钟 45 秒
-
(已锁定)
Build a door cam with a Raspberry Pi4 分钟 4 秒
-
(已锁定)
Explore the Sense HAT emulator3 分钟 49 秒
-
(已锁定)
Use a hard drive with the Raspberry Pi3 分钟 33 秒
-
(已锁定)
Build a temperature-controlled fan for the Raspberry Pi 46 分钟 59 秒
-
(已锁定)
Run COBOL on a Raspberry Pi4 分钟 12 秒
-
(已锁定)
Adafruit IO7 分钟 41 秒
-
(已锁定)
Overclocking the Raspberry Pi7 分钟 4 秒
-
(已锁定)
The boot directory and config.txt7 分钟
-
(已锁定)
Field connection to the Raspberry Pi6 分钟 30 秒
-
(已锁定)
Rotary phone: Microphone hardware4 分钟 34 秒
-
(已锁定)
Rotary phone: Microphone software5 分钟 47 秒
-
(已锁定)
Raspberry Pi 12-megapixel camera7 分钟 27 秒
-
(已锁定)
Text to speech on a chip5 分钟 44 秒
-
(已锁定)
Connect GPS to the Raspberry Pi7 分钟 47 秒
-
(已锁定)
Minecraft and GPIO5 分钟 11 秒
-
(已锁定)
Raspberry Pi Pico5 分钟 31 秒
-
(已锁定)
Use libgpiod instead of sysfs9 分钟 51 秒
-
(已锁定)
Revisiting adding a printer to Raspbian3 分钟 50 秒
-
(已锁定)
Revisiting a temperature controlled fan4 分钟 26 秒
-
(已锁定)
Run R on a Raspberry Pi7 分钟 35 秒
-
(已锁定)
Another motion-activated camera4 分钟 49 秒
-
(已锁定)
Ubuntu desktop4 分钟 38 秒
-
(已锁定)
A web interface to a camera5 分钟 53 秒
-
(已锁定)
Use systemd to start programs at boot6 分钟 55 秒
-
(已锁定)
Find the IP address of your Raspberry Pi5 分钟 2 秒
-
(已锁定)
Contribute to community computing4 分钟 6 秒
-
(已锁定)
BBC BASIC on the Raspberry Pi4 分钟 25 秒
-
(已锁定)
Capacitive touch sensor3 分钟 35 秒
-