Rapid Response AI | High-Speed Object Detection System With Motor Response
by Caleb_Pickett_4 in Circuits > Raspberry Pi
1013 Views, 6 Favorites, 0 Comments
Rapid Response AI | High-Speed Object Detection System With Motor Response
Hello!
In a world of increasing automation, it is becoming easier to create a machine that can "see." But building a machine that can see, decide, and react almost instantly? That is a real challenge.
There is often a significant lag when AI software is linked to physical hardware. We see this all the time in autonomous vehicles: when a smart car detects a pedestrian, it must process that image and signal the mechanical brakes. This process usually takes around 200ms.
To put that in perspective, lets take a look at the physics of a car traveling at 60 mph (~27m/s):
distance = velocity x time
distance = 27 m/s x 0.2 s = 5.4 meters
This means that the car travels over 5 meters before the mechanical system even begins to react.
While we are not going to be building a Tesla in this project, I am going to show you how you can tackle that delay head on. We will do this by making our own AI computer vision system that links to physical output with very little delay.
This Instructable will show you how to build a high-speed AI reflex system that links object detection with motor movement. Instead of building a slow manually powered system, this project will be primarily focusing on making a fast, fully autonomous nervous system: linking the processing power of a Raspberry Pi 5 and the extremely fast YOLOv8 AI network to a precision stepper motor.
Why is this project built for the "Let There Be Speed" contest?
- Processing Speed: In this build we will be taking advantage of the highly optimized YOLOv8 AI model and will be running it on the Raspberry Pi 5 to achieve quick, high accuracy object detection without needing a heavy desktop GPU.
- Reaction Speed: We are going to skip using bulky controllers. The Python code we will write will translate digital labels around objects directly into high-frequency pulses for our stepper motor driver. This results in near-zero delay.
- Mechanical Speed: By utilizing micro stepping, the motor will trade inconsistent brute force for a smooth glide that won't stall when receiving data.
Whether you want to build an automated camera tracker, a door that only unlocks when a certain object is detected, or just want to learn how to make hardware react to computer vision instantly, this is your starting line. Let's begin!
Supplies
The following are the supplies you will need for the project. I have added a short description of how each item will be used, the price, and the exact website that I purchased the item from.
Raspberry Pi
- Raspberry Pi 5 4 GB - This will be the brain of our project. It will be used for running code and our AI.
- Price ~$85. You can buy one here.
- Micro SD Card 64gb - You need a micro SD card to store all the information on the Raspberry Pi.
- Note: If you do not already have one, make sure you get a micro SD to USB adapter. This will be used to "flash" the micro SD on a computer before putting it into the Raspberry Pi.
- Price $16 Buy here.
- Raspberry Pi 5 Cooling Fan - A cooling fan is absolutely necessary if you want the code to run quick and smoothly. The Raspberry Pi will heat up from running the code and AI detection, so we want to make sure that it stays cool and can run everything fast with no issues.
- Price $12 Buy here.
- Keyboard and Mouse - Any keyboard and mouse will do. We will connect these to the Raspberry Pi to allow us to use it like a computer. If you don't have these, you can pick up a keyboard and mouse on Amazon for less than $10.
For The Circuit
- DRV8825 Stepper Motor Driver - This small chip will be the center of our circuit. It takes input from the Raspberry Pi and converts it into signals that the motor can read.
- Price $14 (5 Pack, best value) Buy here.
- Breadboard - We will be using a solderless breadboard to connect our circuit together. They are easy to use and a lot better than having to solder wires together.
- Price $7 Buy here.
- Jumper Wires - These wires are necessary for connecting everything. If you choose to buy from a different link than listed below, make sure to buy an assortment of male-male, female-female, and male-female.
- Price $7 Buy here.
- Capacitor - A capacitor is crucial if you don't want to fry anything. I recommend getting a 100uf 35V capacitor to keep things running smoothly.
- Price $6 Buy here.
- Voltage Reader - We will use a voltage reader to set out DRV8825 motor driver to the correct voltage. This ensures that the driver does not overheat while making sure it produce enough power.
- If you do not already own a screwdriver, I also recommend buying a small Phillips head one. Make sure the tip and shaft are made of metal.
- Price $18 I borrowed one to use for this project, but you can find a cheap one here.
Power Supply
- Raspberry Pi 5V Power Cord - This cord will be used for powering the Raspberry Pi. 5V is the sweet spot for powering the Raspberry Pi.
- Price $12 Buy here.
- 12V Power Supply Cord with Adaptable Tips - This is what we will use to power the motor. Ensure that you buy a cord with multiple tips so that jumper wires can be connected to it.
- Price $15 Buy here.
Video
- Raspberry Pi Camera Module 3 - This is the camera that we will be using to capture video for object detection. Note: the link I have provided comes with a camera cable compatible to the Raspberry Pi 5, however, if you choose to buy from a different link, make sure you purchase a camera ribbon cable that is compatible.
- Price $40-$64 Buy here or buy here if you already have a ribbon cable.
- Micro HDMI to HDMI Cable - This is what we will use to connect the Raspberry Pi to a screen.
- Price $9 Buy here.
- TV/Monitor/Screen - You are going to need a screen to connect to the Raspberry Pi. Anything with an HDMI port will do the job. You can use a TV, computer monitor, or a laptop. I recommend using a monitor or TV.
- If you do not own a TV, computer monitor, or even a laptop, no worries! You can buy a small LCD screen online for a reasonable $35. Buy one here.
Motor
- Nema 17 Stepper Motor - For this project I used a stepper motor. They are high toque and can be used for many other projects. If you don't want to use as strong as a motor, you can buy a smaller "pancake" stepper motor for a cheaper price. I will add link for both options below.
- Price $16 Buy here. (High torque)
- Price $12 Buy here. (Pancake stepper motor)
Attaching Camera to Raspberry Pi
Before we do anything, we are going to want to attach the camera to the Raspberry Pi. This has to be done with the power off, so the start is a great time to do it. I recommend looking at the pictures above to help with following these steps.
Attaching the camera is pretty straightforward.
Pull the little tab on the backside of the camera up very gently until it opens.
Once open, put the large side of the ribbon cable in all the way and then close the clasp. Make sure that the side of the cable with the metal is facing towards the lens of the camera.
Now that the ribbon is attached to the camera we need to attach it to the Pi.
On the Raspberry Pi you should see little slots similar looking to the one on the back of the camera. Similar to before, lift one of the clasps until it opens and put the ribbon cable in. Close the clasp. The side with the metal strips should be facing towards the USB ports.
Please keep in mind, the ribbon cable is breakable. Although it is flexible, it can break if you bend it too much or aren't gentle with it.
Now your camera is connected!
Attaching the Cooling Fan
Now that the camera is attached, you can put the cooling fan on. If you are using the one that I linked in the materials, just peel the paper off of the stickers and stick it on the Raspberry Pi. The sticky foam pads are meant to be stuck to the parts of the Raspberry Pi that get the hottest. (processor, controller, etc.)
Also, make sure that the holes in the corners of the fan are aligned with the holes on the Pi so that it can be screwed in.
Once everything is lined up, use the screws that came with the fan and screw the fan into the Raspberry Pi.
Now that it is locked in place, you can plug the wires from the fan into the fan connector. It is located on the side of the Raspberry next to the metal pins.
See the picture above if you need any reference.
The reason that we are using a cooling fan is because we want everything to run as fast as possible. If the Raspberry Pi's processor and GPU start to get to hot, it will slow things down to avoid frying itself.
Flashing Raspberry Pi OS
"Flashing" the Raspberry Pi just means setting it up and downloading the operating system. To do this all you need is a computer and your micro SD card and USB adapter. This is the only time we will be using a computer in this project. If you do not own a computer, no worries! You can use a friends or go to a public library as most have public use computers. Doing this is easy and only takes a couple minutes. Let's begin.
Insert your micro SD card into the adapter and then plug it into your computer.
Head over to the Raspberry Pi official website.
Go to the top of the website and click on "Software"
Now you will have the option to download Raspberry Pi imager for Windows, macOS, or Linux. Depending on what computer you are using, click on one of those options. I am using a standard desktop computer, so I selected Windows.
Once the file is downloaded- open it.
From here you can refer to the above images I sourced from the getting started with Raspberry Pi website. I recommend following the steps I have written as the website uses some different methods we will not need.
A window will open asking you to "select your Raspberry Pi device." We are using a Raspberry Pi 5, so click on that. Hit next.
You will then be as to "choose operating system." Select Raspberry Pi OS (64-bit). This is the fastest and most efficient operating system. Hit next.
After this you will be asked to "select your storage device." Your micro SD card should already be detected but if it is not you can do it manually.
Once all this is finished you will be asked to fill out some personal information such as making a username and password, connecting to your Wi-Fi, and if you want to enabled Raspberry Pi Connect. I did not enable Raspberry Pi Connect as it is not needed for this project. It basically just allows you to access you Raspberry Pi remotely from other areas.
After this you will have made it to the end. You will be asked to "write image." Click Write. Keep in mind that doing this will wipe the micro SD card to install the operating system. Make sure you don't have anything important on it.
After selecting Write you will have to wait a couple minutes for everything to install and verify. Once everything is done you can click on "Finish." Now we can get started with the code!
Setting Up the Raspberry Pi
Now that your micro SD card has Raspberry Pi OS written on it, you can eject it from the computer and insert it into the micro SD slot on the bottom of the Raspberry Pi. Once the card is in, plug in your Raspberry Pi using the 5V power supply, and connect it to a screen (monitor/TV) using the micro HDMI to HDMI cord. The power port and micro HDMI ports are right next to each other. In the photo above, the power port is circled in blue and the micro HDMI port is circled in red.
Now that your Raspberry Pi is hooked up to a monitor. Plug in your keyboard and mouse to the USB ports. You can now power on your monitor/TV if you have not already done so.
At this point, your Raspberry Pi should be booting up and in a short time you will see your home screen for the Raspberry Pi. We are officially done "setting stuff up" and can get started on actually building this project!
AI Object Detection Code
Now that the Pi is all set up, we are going to add the AI object Detection Code. Doing this on my own led to many, many, many errors, so make sure to follow the instructions below exactly and in order and everything will work smoothly. Also, follow along with the images above.
Note: Installing the AI model took the biggest chunk of time in this project. As it turns out, there is not a single online tutorial or video on YouTube (at least in my hours of searching) that is up to date with the newest version of Python code, which made this a bit of a pain to figure out.
I recommend using this method instead of wasting time looking for a video to explain something that would've worked a couple months ago. I will try my best to be as specific as possible with the code.
Helpful Tip: Copy and pasting the code from this Instructable is your best bet. You can access this Instructable through the built-in Raspberry Pi Chromium Web Browser. It is located at the top of the Raspberry Pi home screen. The icon is a pale blue version of the Google Chrome logo.
Enough yammering. Let's begin.
Step 1. Open Raspberry Pi Terminal.
- At the top of your screen you will see a small black square icon with some lines in it. Once you click on the box, a small, black window will open up. This is the terminal. It's where you can write code for your Raspberry Pi to make it perform different tasks. The terminal is also where you download different things (like high-speed AI models).
Step 2. Updating Everything.
- Before we get into the serious code, lets make sure that everything is up to date. In your terminal you are going to want to paste the following command and hit enter:
- This will update everything and might take a few minutes. Grab a snack, stretch out.
Step 3. Creating Virtual Environment.
- The new Raspberry Pi OS will not let you install packages into a normal terminal. We are going to make a "virtual environment" to keep everything contained. To do this we will start with making a folder. Paste the following then hit enter:
- This creates your vision folder. Now to enter it, paste the following line:
- You should now see ~/vision at the end of your username.
- Now we can make a virtual environment using the following code:
- Now activate it:
- At this point you should see (yolov8-env) at the beginning of your terminal prompt. This is necessary so do not proceed without it.
Step 4. Install the AI libraries.
- Now we are going to install YOLOv8 and OpenCV. Paste the following code:
- This will take anywhere from 10-20 minutes. Eventually you may be asked to type Y or N to finish the install. Type "Y", then hit enter.
- Alright! At this point we should have everything downloaded and in the Raspberry Pi. Now we can make a folder to write our object detection code in. Paste the following line:
- This should bring you into an empty folder. Now you can paste the following code:
How this code works:
This big chunk of code can be broken down into a few main points:
- Initialization: It loads the YOLOv8 "nano" model (yolov8n.pt), which is optimized for speed on small devices, and initializes the Picamera2 library to handle the camera hardware at a resolution of 640x480.
- Frame Capture: Inside a continuous loop, the code captures image data from the camera and makes sure it is in a 3-channel (RGB) format that the AI can process.
- AI Inference: The model(frame) line sends the image to YOLOv8. It only shows high-confidence detections (conf>0.5) and uses results[0].plot() to automatically draw boxes and labels around detected objects.
- Performance Tracking: It calculates FPS (Frames Per Second) by measuring the time elapsed between loop iterations and overlays this number onto the video feed using OpenCV.
- Cleanup: A try...finally block ensures that even if the code crashes or you quit (by pressing 'q'), the camera hardware is properly released and the windows are closed to prevent system errors.
- With the code pasted into the folder, you can now save and exit. Press the following key combinations in order to save and exit your file. (one at a time)
- Ctrl + X (initiates closing)
- Y (saves code)
- Enter (leave)
- This will save your file and bring you back to the main terminal. You can now run all your code with the following line:
Give it a second or two on the first boot, the camera window will open and you can see a live video feed. Face your camera towards you, and you will see a box label around you, labelling you as a person! (unless you are an alien...) The AI may also start picking up objects in the background such as potted plants, water bottles, televisions, remotes, phones, etc.
When I first got this part working, I played with it for a good two hours, maybe more. Take some time to test it on different objects and have fun. Next, we begin with the circuits.
Adjusting Voltage on Stepper Motor Driver
Before starting this step, shut down the Raspberry Pi (using the small button on the end) and disconnect it from power.
Before we fully build our circuit, we are going to need to adjust the voltage on our tiny purple stepper motor driver. Fresh from the package, these drivers are not set to the correct voltage limit. If your current limit remains unchanged, the driver will overheat and fry itself. This happened to me twice while building the project (I did not know you had to adjust the voltage) and it was not ideal to say the least.
But before we dive in, why are we using a "stepper motor driver"?
- The DRV8825 stepper motor driver is optimized for precision, and most importantly, speed. It is essentially a second brain that does another quick process of information before finally moving the motor. Traditionally when making projects like this, many people link the microcontroller directly to the physical output. This causes disorganization, and delay. By using the driver, we are speeding things up significantly and just making the whole system less prone to errors.
To do this you are going to need the following of your materials. Refer to the image above for guidance.
- Jumper wires
- Breadboard
- Capacitor
- DRV8825 stepper motor driver (with mini heatsink)
- 12V power supply with DC Barrel Jack Adapter with Screw Terminal (green tipped with an entry for positive and negative wires)
- Voltage reader and screwdriver.
Start by putting the the heatsink on to the DRV8825 driver. It sticks on like a sticker and should be placed on the black rectangle towards the bottom. This just helps keep the driver cooler and prevent overheating.
Now you can assemble the circuit according to the above image of the circuit. Make sure to read the steps below as well.
- Push the pins of the stepper motor driver into the middle of the breadboard. (See first image)
- Insert two jumper wires into the 12V power supply tip and screw them into place. (See third image)
- Use the jumper wires to connect the pins on the Raspberry Pi to the correct rows on the breadboard.
- Insert the capacitor. (see below for safety)
Important:
- Make sure you connect RESET to SLEEP - In the picture of the circuit, you will see that there is a small wire connecting the reset pin to the sleep pin. If there is an issue with power to the driver, it is usually because you forgot to do this.
- DO NOT PUT THE CAPACITOR IN BACKWARDS - Please do not put the capacitor in the breadboard in an opposite way shown on the image. The side with the stripe down the side should be connected to the GND pin (negative wire from power cord). If the capacitor is put in backwards it can explode. This is not a joke.
Now that the circuit is built, keep the power off for now, and we will calculate the voltage for our driver.
To start, we need to find out how many amps the motor is rated for. There should usually be a small card that comes with your motor that will give you this info.
The voltage limit for the driver is always half of the numerical value of the motor's limit, in volts. For example a motor limited at 1.0 amps means the driver's limit will be 0.5 volts.
By looking at the top of my card, I can tell that my motor is rated for a maximum of 2.0 amps. This means that the voltage of the driver should not exceed 1.0 volts. However, setting the driver to its limit is actually overkill and can still cause it to overheat. To make sure it has enough power and that it does not overheat, I will set the voltage down to 0.6 volts.
To do this, you are going to want to connect power to the circuit we just built. Plug in the 5V power cord to the Raspberry Pi and plug in the 12V power cord to the positive and negative tip that has jumper wires coming out of it.
- If you need visual assistance for the next steps, look at the 5th and 6th images. The sixth image was borrowed from MakerGuides.Com
With the power on, you can turn on the voltage reader. Connect the positive side to the shaft of the screwdriver and touch the negative side to the bottom right GND pin on the driver. With the screwdriver side, touch it to the screw in the top left of the driver (the potentiometer) and turn the screw slowly either way until the voltage reader says your desired voltage limit.
Now the voltage limit is set!
Fully Assembling the Circuit
Now that the voltage of the DRV8824 stepper motor driver has been adjusted, we can now begin on the final part of the project - linking the AI object recognition to a physical output!
BEFORE ADDING ON TO THE CIRCUIT, DISCONNECT ALL POWER.
Since most of our circuit is already built, we just have to add a few more connections. Refer to the image above to add the following:
- Microstepping
- The three extra wires added that run from the Raspberry Pi to the M0, M1, and M2 pins on the driver will allow us to enable microstepping. This allows us to move the motor smoother, which increases accuracy and speed.
- Connecting the Motor
- The wires from the motor will connect to the B2, B1, A1, and A2 pins on the driver. The name of this pins varies depending on where you get the driver from, but the first two are for the first coil and the second two are for the second coil. A "coil" is just a term for a pair of wires on the motor. The datasheet that comes with the motor will tell you what each coil is. Common combinations are often:
- Coil 1 - Black and Green
- Coil 2 - Red and Blue
- Important note:
- When I first got the motor, there was an adapter at the end of the wire. A quick google search told me I could just cut it off... Please do not cut the adapter off. You can simply plug jumper wires into it. Or, if you do not have an adapter, you can do what I do and just twist the wire and stick it in the breadboard. See image 2.
AI to Motor Code
Now that our circuit is built, you can power on the Raspberry Pi, connect the keyboard and mouse. And plug in the HDMI cord to a screen.
Just like earlier we will be opening the terminal to start our code.
First we will want to get back into our virtual environment and vision folder before we do anything. Type the following lines in order.
-enter the vision folder
-enter virtual environment
Now we are going to install gpiozero. This just allows our Raspberry Pi to use its pins. Paste the following line:
Once this is done installing, we will just create a folder:
Once that folder opens paste this big chunk of code:
Breakdown of how the code works:
System Setup: We start off by importing the necessary libraries for computer vision, AI, camera control, and hardware (GPIO) control.
Hardware Initialization: Configures the Raspberry Pi pins to control a stepper motor (including microstepping) and defines the spin_degrees and return_home functions to move the motor and track its exact position.
AI & Camera Initialization: Loads the YOLOv8 AI model for object detection and turns on the Raspberry Pi camera feed.
The Main Loop (runs continuously):
- Capture Frame: Grabs the current image from the live camera feed and formats the colors so YOLO can read it.
- Detect Objects: Feeds the image into the YOLO model to find and draw bounding boxes around any recognized objects.
- Calculate FPS: Figures out how fast the system is processing (Frames Per Second) and prints that number on the screen.
- Trigger Hardware: Looks specifically for bottles and cell phones in the AI's results.
- If it sees a bottle, the motor spins 90 degrees clockwise, waits 5 seconds, and returns to its starting point.
- If it sees a cell phone, the motor spins 90 degrees counter-clockwise, waits 5 seconds, and returns to its starting point.
- Display Video: Shows the live, annotated video feed on your screen.
- Check for Exit: Listens for you to press the 'q' key. If you do, it breaks out of the loop.
Safe Shutdown (finally block): Once the loop ends, it ensures the motor safely returns to the "home" position, turns off the camera, cuts power to the motor pins, and closes the video window.
With the code now pasted into the file, save and close everything out by pressing the following:
- Ctrl + X (initiates closing)
- Y (saves code)
- Enter (leave)
Now for the big moment. Run the code:
You should see your camera window open and the object detection go live. Hold up a bottle or phone, and you will see the AI almost instantly detect the item and move the motor. The motor then waits then returns home.
Here are a few ways you can modify the code:
- Adding different/more items - To add more items, just copy the code for detecting the bottle and replace "bottle" with a different item. If you look at the above image, I have included a chart of what items this AI model is trained on.
- Changing degrees/direction - To change the degrees the motor spins just change the spin_degrees(90) to a different number. Example: spin_degrees(180). To change direction, (counter-clockwise or clockwise) you can switch out the code the "ccw" in the spin_degrees code to "cw" or vise versa.
- Changing speed - To change the speed of the motor, change the numerical values of the sleep code towards the beginning of the code. The higher the number, the slower the motor. You would edit the following block:
Have fun with it. You can now make a multitude of projects with this high speed AI to motor code. Here are a few ideas:
- Robot car that looks for certain objects.
- Box that only unlocks when a key item is detected.
- Security alarm that detects people.
- Smart lights that turn on when a person is in the room and off when someone leaves.
The possibilities are endless.
Every time you boot up your Raspberry Pi, you can run this code again by just doing the following:
-enter vision folder
-enter virtual environment
-run the code.
Make sure to write these lines in order and one at a time.
Conclusion
We started this project by looking at the 5 meter gap created by typical processing delays in autonomous systems. By combining the incredible processing power of the Raspberry Pi 5 with the efficiency and accuracy of YOLOv8 and the precision of the DRV8825, you’ve built a system that narrows that gap significantly.
How We Achieved a High-Speed AI Response system:
- Optimized Vision: By running the YOLOv8 AI model on the Raspberry Pi 5, we built an incredibly accurate and high speed computer vision system.
- Efficient Code: I have written, revised, and re-written the code many times to reduce delay and keep things as error-free and fast as possible.
- Hardware Precision: By taking the extra effort to add in the DRV8825 stepper motor driver to our circuit, we increased output processing speed and made the flow from AI to motor much smoother.
The true power of this build isn't just in detecting bottles or phones; it’s in the autonomous nervous system and how we made it react extremely fast. You now have the template to build machines that don't just observe the world, but interact with it in real-time.
Thank you very much for taking your time to read this Instructable. I hope you enjoyed learning about this project as much as I enjoyed building it. I encourage you to try to build this on your own and work on your own high speed AI powered projects.