From the course: Raspberry Pi Weekly
Code a stepper motor - Raspberry Pi Tutorial
From the course: Raspberry Pi Weekly
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.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
The Raspberry Pi family3m 53s
-
(Locked)
GPIO male to female jumpers3m 30s
-
(Locked)
Weatherproofing Pi4m 19s
-
(Locked)
Power and batteries3m 57s
-
(Locked)
IR rangefinder project2m 26s
-
(Locked)
Clock project6m 20s
-
(Locked)
Wire a stepper motor3m 38s
-
Code a stepper motor3m 37s
-
(Locked)
gpiozero library4m 50s
-
(Locked)
Take a screenshot4m 26s
-
(Locked)
Analog to digital2m 38s
-
(Locked)
Autostart file setup4m 24s
-
(Locked)
Simple connection to a speaker2m 43s
-
(Locked)
Red LED, RGB LED, blink, and glow3m 23s
-
Connect input to output: Range finder to LED2m 4s
-
(Locked)
Raspberry Pi Desktop on a laptop4m 10s
-
(Locked)
Programming with Node-RED6m 15s
-
(Locked)
Programming IoT with IFTTT5m 24s
-
(Locked)
Update the Raspberry Pi with apt-get3m 3s
-
(Locked)
A clean and simple web server5m 23s
-
(Locked)
Use SSH to control the Raspberry Pi3m 28s
-
(Locked)
Camera control with raspistill4m
-
(Locked)
GPIO pinout charts3m 53s
-
(Locked)
Back up the Raspberry Pi3m 1s
-
(Locked)
Use SmartSim to explore logic circuits4m 47s
-
(Locked)
Raspberry Pi 46m 29s
-
(Locked)
Change your default password2m 44s
-
(Locked)
Mathematica and general-purpose input/output (GPIO)6m 7s
-
(Locked)
Build a Raspberry Pi private cloud8m 40s
-
Dealing with undervoltage3m 50s
-
(Locked)
Play music with Sonic Pi5m 53s
-
(Locked)
Add a printer to Raspberry Pi3m 38s
-
(Locked)
Use Screenly for web kiosks5m 59s
-
(Locked)
Use raspivid for Raspberry Pi Videos4m 55s
-
(Locked)
Build an MQTT publisher6m 33s
-
(Locked)
Build an MQTT client4m 5s
-
(Locked)
Connect to MQTT with Node-RED9m 55s
-
(Locked)
Build an MQTT broker4m 44s
-
(Locked)
A power switch for the Raspberry Pi3m 57s
-
(Locked)
Input polling vs. interrupts5m 41s
-
(Locked)
GPIO Pi headers2m 34s
-
(Locked)
High-grade audio for the Raspberry Pi4m 34s
-
(Locked)
Program the Sense HAT with Node-RED6m 54s
-
(Locked)
Remote GPIO6m 5s
-
(Locked)
Framboisedorf4m 6s
-
(Locked)
Use Kano OS5m 15s
-
(Locked)
What's new with Scratch 3.0?2m 57s
-
(Locked)
Controlling high voltages with the Raspberry Pi4m 53s
-
(Locked)
Rotary phone: Intro and switches3m 12s
-
(Locked)
Rotary phone: Ringer2m 44s
-
(Locked)
Rotary phone: Speaker5m 28s
-
(Locked)
Piscope5m 33s
-
Cron on the RPI7m 45s
-
(Locked)
Keep your plants watered with a Raspberry Pi4m 42s
-
(Locked)
Grove HAT4m 45s
-
(Locked)
Build a door cam with a Raspberry Pi4m 4s
-
(Locked)
Explore the Sense HAT emulator3m 49s
-
(Locked)
Use a hard drive with the Raspberry Pi3m 33s
-
(Locked)
Build a temperature-controlled fan for the Raspberry Pi 46m 59s
-
(Locked)
Run COBOL on a Raspberry Pi4m 12s
-
(Locked)
Adafruit IO7m 41s
-
(Locked)
Overclocking the Raspberry Pi7m 4s
-
(Locked)
The boot directory and config.txt7m
-
(Locked)
Field connection to the Raspberry Pi6m 30s
-
(Locked)
Rotary phone: Microphone hardware4m 34s
-
(Locked)
Rotary phone: Microphone software5m 47s
-
(Locked)
Raspberry Pi 12-megapixel camera7m 27s
-
(Locked)
Text to speech on a chip5m 44s
-
(Locked)
Connect GPS to the Raspberry Pi7m 47s
-
(Locked)
Minecraft and GPIO5m 11s
-
(Locked)
Raspberry Pi Pico5m 31s
-
(Locked)
Use libgpiod instead of sysfs9m 51s
-
(Locked)
Revisiting adding a printer to Raspbian3m 50s
-
(Locked)
Revisiting a temperature controlled fan4m 26s
-
(Locked)
Run R on a Raspberry Pi7m 35s
-
(Locked)
Another motion-activated camera4m 49s
-
(Locked)
Ubuntu desktop4m 38s
-
(Locked)
A web interface to a camera5m 53s
-
(Locked)
Use systemd to start programs at boot6m 55s
-
(Locked)
Find the IP address of your Raspberry Pi5m 2s
-
(Locked)
Contribute to community computing4m 6s
-
(Locked)
BBC BASIC on the Raspberry Pi4m 25s
-
(Locked)
Capacitive touch sensor3m 35s
-