Skip to content

Commit 1b402aa

Browse files
committed
Add more descriptive comments to examples
1 parent 1279dc2 commit 1b402aa

3 files changed

Lines changed: 66 additions & 45 deletions

File tree

DRV8825/DRV8825/Examples/drv8825-acceleration.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,44 @@
33
# BRIEF: An example of accelerating stepper motor.
44
# LAST UPDATED: 2025-05-27
55

6-
from drv8825 import DRV8825
7-
from time import sleep
6+
from drv8825 import DRV8825 # Import the DRV8825 motor driver module
7+
from time import sleep # Import sleep function for timing delays
88

9-
DIR_PIN = 4 #GPIO4
10-
STEP_PIN = 5 #GPIO5
9+
# Define GPIO pins for motor control
10+
DIR_PIN = 4 # GPIO pin used for direction control
11+
STEP_PIN = 5 # GPIO pin used for step pulses
1112

12-
motor=DRV8825()
13+
# Create a DRV8825 motor instance
14+
motor = DRV8825()
15+
16+
# Initialize the motor with the specified DIR and STEP pins
1317
motor.begin(DIR=DIR_PIN, STEP=STEP_PIN)
18+
19+
# Set the number of steps the motor takes per full rotation (usually 200 for a 1.8° stepper motor)
1420
motor.setStepsPerRotation(200)
21+
22+
# Set the length of each step pulse in microseconds
1523
motor.setStepPulseLength(1000)
24+
25+
# Set motor rotation direction to clockwise
1626
motor.setDirection(DRV8825.CLOCK_WISE)
27+
28+
# Enable the motor driver (turns on output)
1729
motor.enable()
1830

1931
print("Accelerating")
2032

33+
# Gradually increase the step frequency (i.e., speed up the motor)
34+
accelerationFactor = 1
35+
while accelerationFactor < 2000:
36+
motor.step() # Send a single step pulse to the motor
37+
sleep(1 / accelerationFactor) # Delay inversely proportional to the factor (higher = faster)
38+
accelerationFactor += 1 # Increase acceleration factor
39+
print(accelerationFactor) # Print current acceleration factor
2140

22-
accelerationFactor=1
23-
while accelerationFactor<2000:
24-
motor.step()
25-
sleep(1/accelerationFactor)
26-
accelerationFactor+=1
27-
print(accelerationFactor)
28-
29-
while accelerationFactor>1:
30-
motor.step()
31-
sleep(1/accelerationFactor)
32-
accelerationFactor-=1
33-
print(accelerationFactor)
41+
# Gradually decrease the step frequency (i.e., slow down the motor)
42+
while accelerationFactor > 1:
43+
motor.step() # Send a single step pulse
44+
sleep(1 / accelerationFactor) # Delay increases as acceleration factor decreases
45+
accelerationFactor -= 1 # Decrease acceleration factor
46+
print(accelerationFactor) # Print current acceleration factor
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
# FILE:ws2812b-blink-example.py
1+
# FILE: ws2812b-blink-example.py
22
# AUTHOR: Soldered
33
# BRIEF: A simple example of blinking WS2812b LED's
44
# LAST UPDATED: 2025-05-26
55

6-
import machine
7-
import neopixel
8-
import time
6+
import machine # For accessing GPIO pins
7+
import neopixel # Library to control WS2812b (NeoPixel) LEDs
8+
import time # For delay/sleep functions
99

1010
# === Configuration ===
11-
PIN = 33 # GPIO pin
12-
NUM_PIXELS = 24 # Number of LEDs on-board (Change as needed)
11+
PIN = 33 # GPIO pin connected to the data line of the WS2812B LED strip
12+
NUM_PIXELS = 24 # Number of LEDs in the strip (adjust this to match your setup)
1313

1414
# === Setup ===
15+
# Create a NeoPixel object on the specified GPIO pin with the specified number of LEDs
1516
np = neopixel.NeoPixel(machine.Pin(PIN), NUM_PIXELS)
1617

17-
# === Function to set color (R, G, B) ===
18+
# === Function to set all LEDs to a specified color (R, G, B) ===
1819
def set_color(r, g, b):
19-
for i in range(NUM_PIXELS):
20-
np[i] = (r, g, b)
21-
np.write()
20+
for i in range(NUM_PIXELS): # Iterate over each LED
21+
np[i] = (r, g, b) # Set the color for the current LED
22+
np.write() # Update the strip (could be moved outside loop for efficiency)
2223

2324
# === Blink loop ===
25+
# Continuously blink all LEDs on and off
2426
while True:
25-
set_color(255,255, 255) # White color
26-
time.sleep(0.5)
27-
set_color(0,0,0) #Turn off
28-
time.sleep(0.5)
27+
set_color(255, 255, 255) # Turn all LEDs on to full white
28+
time.sleep(0.5) # Wait 0.5 seconds
29+
set_color(0, 0, 0) # Turn all LEDs off (black)
30+
time.sleep(0.5) # Wait 0.5 seconds
31+
2932

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
# FILE:ws2812b-simple-fade-example.py
1+
# FILE: ws2812b-simple-fade-example.py
22
# AUTHOR: Soldered
33
# BRIEF: A simple fade animation on WS2812b LED's
44
# LAST UPDATED: 2025-05-26
55

6-
import machine
7-
import neopixel
8-
import time
6+
import machine # For accessing hardware like GPIO
7+
import neopixel # NeoPixel/WS2812B control module
8+
import time # For delays
99

1010
# === Configuration ===
11-
PIN = 33 # GPIO pin
12-
NUM_PIXELS = 24 # Number of LEDs on-board (Change as needed)
11+
PIN = 33 # GPIO pin connected to the data line of the WS2812B strip
12+
NUM_PIXELS = 24 # Number of LEDs in the strip (adjust to your setup)
1313

1414
# === Setup ===
15+
# Initialize NeoPixel object on the specified pin with the given number of LEDs
1516
np = neopixel.NeoPixel(machine.Pin(PIN), NUM_PIXELS)
1617

1718
# === Fade Loop ===
19+
# This loop creates a moving red gradient (fading tail) along the LED strip
1820
while True:
19-
for j in range (NUM_PIXELS):
20-
time.sleep(0.01)
21-
for i in range (NUM_PIXELS):
22-
time.sleep(0.01)
23-
if j+i<NUM_PIXELS:
24-
np[i+j]=(i*8,0,0)
21+
for j in range(NUM_PIXELS): # Outer loop: shift the animation forward one LED at a time
22+
time.sleep(0.01) # Short delay to control overall speed
23+
for i in range(NUM_PIXELS): # Inner loop: calculate and set brightness for each LED
24+
time.sleep(0.01) # Additional delay for smoother visual effect
25+
26+
# Calculate LED index based on current position (j) and brightness position (i)
27+
if j + i < NUM_PIXELS:
28+
np[i + j] = (i * 8, 0, 0) # Set red intensity based on position (i), green and blue = 0
2529
else:
26-
np[(i+j)-NUM_PIXELS]=(i*8,0,0)
27-
np.write()
30+
np[(i + j) - NUM_PIXELS] = (i * 8, 0, 0) # Wrap around when index exceeds strip length
31+
32+
np.write() # Update the LED strip with new values

0 commit comments

Comments
 (0)