Create a Lazy Susan Using a Motor and Raspberry Pi
by Kacey Meier in Circuits > Raspberry Pi
424 Views, 6 Favorites, 0 Comments
Create a Lazy Susan Using a Motor and Raspberry Pi
Welcome to the robotic world of culinary comfort and supreme convenience! Are you sick of straining your arms to transfer plates around the dinner table? Are you tired of being taken away from your delicious meal to pass the rolls? Have you ever dreamed of a spinning device to do this chore?
By the end of this tutorial, you will have learned how to wire a motor and motor driver to a Raspberry Pi, install viam-server on your Raspberry Pi, and how to configure your robot in the Viam app to create your own Lazy Susan for your dinner table!
You will also learn to take your creation a step further and fine-tune the controls of your Lazy Susan using the Python motor API methods.
[ Posted by Sierra Guequierre on behalf of Kacey Meier ]
Supplies
Hardware
This project requires the following hardware:
- Raspberry Pi
- microSD card
- microSD card reader
- DC motor (for example this motor)
- Motor driver (for example this driver)
- Appropriate flange coupler for your motor (for example 6mm flange coupler)
- M3 Screws to fit the flange coupler to the board
- Jumper wires
- Stranded wire for the motors (for example 16 gauge stranded wire)
- A circular board or plate (for example 14 inch wood round board)
- A power supply for the motor driver
- A power supply for the Raspberry Pi
Tools
You will also need the following tools:
- Philips head screwdriver
- Double-sided tape (optional, but recommended)
- Wire cutters/scissors
Hardware Setup
A brushed DC motor is a motor that converts electrical current into mechanical energy. In a brushed DC motor, the rotor spins 180-degrees when an electric current is applied. In order to travel beyond the initial 180 degrees, the poles of the electromagnet must flip. Carbon brushes contact the stator as the rotor spins, flipping the magnetic field and enabling the rotor to spin 360-degrees continuously. The brushed motor we are using has a high starting torque which means it can reach a high speed quickly, it is low cost, and can sustain a larger load, perfect for a Lazy Susan!
A motor driver is a device that takes signals from your board and sends power to a motor based on those signals. Motor drivers allow you to start and stop the motor, select clockwise or counterclockwise rotation, and speed it up or slow it down.
A board is the hardware that sends digital signals to control your robot. Your board allows you to send PWM (pulse width modulation) signals to the motor driver to control the motor speed. PWM controls motor speed by sending electrical current in pulses - the more frequently pulses are sent in a given time period, the faster the motor will move. The board sends PWM signals to the motor driver through GPIO (general-purpose input/output) pins, which are digital pins that the board provides to allow you to flexibly communicate with various devices.
You will also use the board to deploy code, run viam-server, and connect your robot to the internet and the cloud.
Use the following diagram to wire your hardware together. Make sure your board is turned off and unplugged before wiring!
The Raspberry Pi and the 12V power supply share the same GND on the L298N motor driver. The Raspberry Pi is powered by its own 5V power cable.
Connect the flange coupler to your motor, and use the M3 screws and screwdriver to fix the flange coupler to your circular board or plate.
Turn on the Raspberry Pi and move on to setting up your software.
Configure Your Robot
If you have not already done so, follow the Raspberry Pi Setup Guide to prepare your Pi to connect your robot to the Viam app. Once your Pi is ready, ssh into it from your computer.
In your web browser, go to the Viam app and create a new robot instance. Name your robot whatever you like and head to the Setup tab. On your Pi, follow the steps there to download the Viam app config and download and install viam-server. Wait a moment until your robot connects to the Viam app.
Then, use the Setup tab to configure your robot according to the mode you have selected in the upper-left hand corner:
Config Builder Mode:
Configure the board
First, configure your board component:
- Navigate to the Config tab of your robot’s page in the Viam app. Click the Components subtab, then click Create component in the lower-left corner.
- Select board for type and pi for model.
- Enter local as the name for your board, then click Create.
Configure the motor
Next, add a motor component:
- Navigate to the Components subtab and click Create component in the lower-left corner.
- Select motor for type and gpio for model. Enter dcmotor as the name for your motor, then click Create.
After creating your motor component, fill in the required attributes for a gpio motor:
- For board, click the dropdown box and select local (or whatever you named your board!).
- For Max RPM, enter 100. RPM stands for revolutions per minute: it’s roughly a measure of how fast any machine is operating at a given time. Enter 100 so the motor moves at a moderate speed. You can fine tune this later in our custom code.
- Toggle the slider to In1/In2 motor type. The specific driver you are using expects this pin configuration.
- During wiring, you connected pins on the Pi to two ports called IN1 and IN2, and a third pin to ENA. When you toggle the In1/In2 motor type, the UI opens three dropdown fields below it. Use these to specify the board pins that each motor driver pin is wired to:
- A/In1: use 16 GPIO 23
- B/In2: use 18 GPIO 24
- PWM: use 22 GPIO 25 - this is the pin you attached the PWM (pulse-width modulation) jumper wire to. On the motor driver we used, it is labeled as ENA.
- For Depends On select local.
- This ensures that the board is initialized before the motor.
- Click Save Config to save your config.
Raw JSON Mode:
- On the Raw JSON tab, replace the configuration with the following JSON configuration for your board and motor:
{
"components": [
{
"name": "local",
"model": "pi",
"type": "board",
"namespace": "rdk",
"attributes": {},
"depends_on": []
},
{
"name": "dcmotor",
"model": "gpio",
"type": "motor",
"namespace": "rdk",
"attributes": {
"pins": {
"a": "16",
"b": "18",
"pwm": "22"
},
"board": "local",
"max_rpm": 100
},
"depends_on": ["local"]
}
]
- Click Save Config to save your config.
Test the Motor in the Viam App
Navigate to the Control tab and click on the top of the dcmotor card to open the motor controls. You can change the direction of rotation of the motor by toggling the Forwards and Backwards button. You can also adjust how fast the motor rotates by adjusting the Power % slider.
Hit the RUN button when you’re ready and check that your motor runs!
Adjust your settings and test your motor to find the perfect settings that suit your Lazy Susan prototype. When you’re done testing your motor, press the STOP button in the upper right corner to stop your motor.
Use the Python SDK to Control the Motor
SSH into your Pi or use your existing SSH session to install the pip package manager. Run the following command and type “yes” when it asks if you want to continue:
sudo apt install python3-pip
The Viam Python SDK allows you to write programs in the Python programming language to operate robots using Viam. To install the Python SDK on your Raspberry Pi, run the following command in your existing ssh session to your Pi:
pip3 install viam-sdk
Installing with pip ensures you are using the latest version of viam-sdk, and makes updating it in the future easy.
On the Viam app, select the Code sample tab and set Python as the language.
By default, the sample code does not include your robot location secret. We strongly recommend that you add your location secret as an environment variable and import this variable into your development environment as needed.
To show your robot’s location secret in the sample code, toggle Include secret on the Code sample tab. You can also see your location secret on the locations page.
Do not share your location secret, part secret, or robot address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your robot, or to the computer running your robot.
Copy the code:
Create a new python file called lazysusan.py on your Raspberry Pi and paste the copied code into it:
nano turnsusan.py
Press CTRL-X to save and exit. Enter y to confirm, and then hit return to accept the same filename.
Next, run the following command to check the connection, and to see if there are any errors:
python3 turnsusan.py
The output should show a list of resources:
If there are no errors, you have successfully connected to your robot and run some code!
Use Motor API Methods
Now let’s move on to write code to fine tune the control of your motor. It would be nice if the lazy susan rotated slowly so people can grab some food. Head to the Motor API in the Python SDK documentation.
SetPower adjusts speed by adjusting the power to the motor. This may feel familiar from when you were testing the hardware in the Viam app and adjusted the Power % and Forward and Backward toggle. Instead of using a toggle to adjust the motor direction, you now use either positive or negative numbers up to 100.
Reference the SetPower API Method example for more information.
GoFor allows you to control how fast the motor spins by allowing you to set the revolutions per minute (or RPMs). This allows you to have more control over how fast and how long you spin the motor for. If you want to have your motor spin backwards, one of these parameters (not both) needs to be negative.
If you have a project that needs to be below a certain speed, you can set the Max RPM in the Viam app as we did during configuration. One extra “0” in your API method could mean you’re picking sunflower seeds from every corner of the room at midnight.
For the purposes of this tutorial, let’s start with the SetPower method and assume that we need the Lazy Susan to run for an undetermined amount of time.
Open the turnsusan.py file with nano within your terminal.
nano turnsusan.py
You can start by adding this call to set_power to your main function above the line that closes the robot connection with robot.close():
# Use the set power method before you close the code loop
await dc_motor.set_power(power=0.2)
Enter CTRL+X to save and exit: press y when prompted to accept changes, and the return key to accept the original filename.
Now, run your code:
python3 turnsusan.py
Your Lazy Susan should now spin at the speed you indicated. Because we didn’t set any parameters for how long this will run for, we will need to stop it using the Viam app button on the Control tab for now.
However, there is also an API method you can use to Stop the motor!
Add the following import statement at the top of your code:
# Import time to add a sleep function to the top of your code
import time
Then add the following code underneath the code that sets the power of the motor:
# Wait 10 seconds to make the motor spin for 10 seconds
time.sleep(10)
# Stop the motor
await dc_motor.stop()
If you would like to have the same effect but use even less code, you can use the GoFor API Method. Remove the code you have just added or comment it out and add the following code instead:
# await dc_motor.set_power(power = 0.2)
# time.sleep(10)
# await dc_motor.stop()
# Turn the motor 7.2 revolutions at 60 RPM.
await dc_motor.go_for(rpm=60, revolutions=7.2)
Enter CTRL+X to save and exit. Press Y when prompted to accept changes, and the return key to accept the original filename.
You have now successfully completed the code for your Lazy Susan and have learned how to turn your motor using the Viam app and with code using the Python SDK.
Now you can go ahead and put your robot together with the turntable. Connect the wood to your motor and decorate it as you wish to make your Lazy Susan a hit at your next dinner party!
Next Steps
This is not where your Lazy Susan project has to end - here are some ideas for adding more components to this project:
- An input controller component so you can turn and stop the Lazy Susan with a gamepad.
- A camera component and an ML model that recognizes your friends’ faces and stops the Lazy Susan precisely where they can reach their favorite food, or takes their photo as it rotates, so you all can have candid memories.
- A sensor component, so your Lazy Susan only spins when it detects movement around it.
Or you can head over to our Tutorials page and try one of our other tutorials to continue building robots.
Let us know how you do and make sure to show off your project in our Community Discord!