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.
Requirements
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 machine. 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!
IMPORTANT: 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.
Software setup
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.
Add a new machine in the Viam app. Then follow the setup instructions to install viam-server
on the computer you’re using for your project and connect to the Viam app. Wait until your machine has successfully connected.
Configure your robot
On your machine's CONFIGURE tab:
- Add the pi board component. Enter
local
as the name for your board, then click Create. - Next, add a motor component: Select
motor
for type andgpio
for model. Enterdcmotor
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. Enter100
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.
Save your configuration.
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
pip
is a package-management system written in Python and is used to install and manage software packages and their dependencies during deployment.
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:
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:
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.
API KEY AND API KEY ID: By default, the sample code does not include your machine API key and API key ID. We strongly recommend that you add your API key and API key ID as an environment variable and import this variable into your development environment as needed.
To show your machine’s API key and API key ID in the sample code, toggle Include secret on the CONNECT tab’s Code sample page.
Caution: Do not share your API key or machine address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your machine, or to the computer running your machine.
Copy the code into a new python file called lazysusan.py
on your Raspberry Pi and paste the copied code into it:
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:
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.
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()
:
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:
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:
Then add the following code underneath the code that sets the power of the motor:
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:
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!