Learn how to create a program to vary the position of the servomotor from 0º to 180º by turning the potentiometer.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 30 min.
MATERIALS:
- 1 Potentiometer
- 1 Servomotor
- 1 USB – Micro USB cable
- Computer
The Mini Lab will have to be built according to the instructions manual.
CONNECTIONS:
- Connect the potentiometer to the analog port A1 of the Build&Code 4in1 board.
- Connect the servomotor to the digital port 4 of the Build&Code 4in1 board.
PROGRAMMING CODE:
You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.
Arduino Code
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- Open the Arduino program and copy the following program in it:
#include <Servo.h> // SERVOMOTOR LIBRARY Servo motor; // SERVOMOTOR VARIABLE int pot = 0, pinpot = A1; // POTENTIOMETER PINS VARIABLE float degree; // SERVOMOTOR DEGREES VARIABLE void setup() { // put your setup code here, to run once: // SERVOMOTOR CONFIGURATION motor.attach (9); } void loop() { // put your main code here, to run repeatedly: // REPLICATE THE ROTATION OF THE POTENTIOMETER WITH THE SERVOMOTOR DEGREES pot = analogRead (pinpot); // READING OF THE POTENTIOMETER VALUES degree = map (pot, 0,1023, 10, 180); //SCALE THE POTENTIOMETER VALUES WITH THE SERVOMOTOR DEGREES motor.write (degree); }
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
Code for the visual programming software by blocks compatible
- Download and install the program.
- Open the software and copy the following code. Use the following image as a guide:
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
Bitbloq code
- Download Bitbloq and install the Web2board app.
- Open the software and copy the following code.
- Hardware
- Software
- Hardware
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
RESULT OF THE EXERCISE:
When turning the potentiometer, you will move the servomotor from 0º to 180º.