PROJECT Nº 4: Control the servomotor

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:

  1. Connect the potentiometer to the analog port A1 of the Build&Code 4in1 board.
  2. 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 

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. 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);  
    }
    
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. 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

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code 

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware
    • Software
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. 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º.

1 0

0