Skip to content
  • HOME
  • Shop
    • IOT & Wireless
      • Module EPS32
      • Shield Board
      • Module ESP8266
      • Module Lora
      • Module STM32
      • Wifi Lainnya
    • Sensor Modules
      • Sensor Proximity
      • Sensor Tegangan
      • Sensor Temp & Humidity
      • Sensor Ultrasonic
      • Sensor Modules
      • Sensor Gas / Udara
      • Sensor Liquid / Water
      • Sensor GPS Compas
      • Sensor Infrared / Light
      • Sensor Kits
      • Sensor Motion
      • Sensor Lainnya
      • Sensor Laser Range
    • Arduino Ecosystem
      • Arduino Official
      • Arduino Compatible
      • Arduino Kits
      • Camera Arduino
      • Komponen Arduino
      • Electronics
      • LCD Display Arduino
    • Motor & Servo
      • Servo Controller
      • Servo Modules
      • Stepper Driver
      • DC Motor
    • RFID
      • RFID Card
      • RFID Smart Reader
  • SBC
    • Nvidia AI
      • Nvidia AI Board
      • Camera Nvidia AI
      • Module & Aksesoris
      • Nvidia Display
    • Raspberry Pi
      • Raspberry Pi Board
      • Raspberry Pi Kits
      • Raspberry Pi Camera
      • Raspberry Pi Accesories
      • Raspberry Pi MCU
      • Raspberry Pi Case
      • Raspberry Pi Display
    • BBC Microbit
      • Microbit Board
      • Microbit Kits
      • Microbit Robots
      • Shield IO Microbit
    • SBC Raxda
    • SBC Odorid
    • Orange Pi
    • LCD Display
  • Wiki
  • Blog
  • Contact
  • HOME
  • Shop
    • IOT & Wireless
      • Module EPS32
      • Shield Board
      • Module ESP8266
      • Module Lora
      • Module STM32
      • Wifi Lainnya
    • Sensor Modules
      • Sensor Proximity
      • Sensor Tegangan
      • Sensor Temp & Humidity
      • Sensor Ultrasonic
      • Sensor Modules
      • Sensor Gas / Udara
      • Sensor Liquid / Water
      • Sensor GPS Compas
      • Sensor Infrared / Light
      • Sensor Kits
      • Sensor Motion
      • Sensor Lainnya
      • Sensor Laser Range
    • Arduino Ecosystem
      • Arduino Official
      • Arduino Compatible
      • Arduino Kits
      • Camera Arduino
      • Komponen Arduino
      • Electronics
      • LCD Display Arduino
    • Motor & Servo
      • Servo Controller
      • Servo Modules
      • Stepper Driver
      • DC Motor
    • RFID
      • RFID Card
      • RFID Smart Reader
  • SBC
    • Nvidia AI
      • Nvidia AI Board
      • Camera Nvidia AI
      • Module & Aksesoris
      • Nvidia Display
    • Raspberry Pi
      • Raspberry Pi Board
      • Raspberry Pi Kits
      • Raspberry Pi Camera
      • Raspberry Pi Accesories
      • Raspberry Pi MCU
      • Raspberry Pi Case
      • Raspberry Pi Display
    • BBC Microbit
      • Microbit Board
      • Microbit Kits
      • Microbit Robots
      • Shield IO Microbit
    • SBC Raxda
    • SBC Odorid
    • Orange Pi
    • LCD Display
  • Wiki
  • Blog
  • Contact

Analog Sensor

2
  • DHT11
  • Analog Infrared CO2 Sensor For Arduino (0~5000 ppm)

Arduino Basic Kit

1
  • Sensor Ultrasonic HC SR04

IOT Basic Dengan NodeMCU ESP8266

1
  • Setting Blynk V2

Raspberry Pi Pico Basic

15
  • Programming LED Raspberry Pi Pico
  • LED chaser Dengan Raspberry Pi Pico
  • LED Control Dengan Push Button Di Pi Pico
  • Mengkontrol LED Dengan Potensio Di Pi Pico
  • Lampu Mengalir Warna Warni Raspberry Pi Pico
  • Buzzer Dengan Raspberry Pi Pico
  • HC SR501 Pir Motion Sensor Gerak
  • Control Servo SG90 Motor Dengan Raspberry Pi Pico
  • Kontrol Servo SG90 Dengan Potensio Di PI PICO
  • HC-SR04 ultrasonic distance sensor Jarak
  • HC-SR04 Ultrasonic Di LCD 1602 I2C Pi Pico
  • DHT11 Sensor Suhu Dan Kelembaban
  • LCD 1602 I2C Display Ke Raspberry Pi Pico
  • Raspberry Pi Pico – DHT11
  • Menampilkan DHT11 Di LCD 1602 I2C Pada Pi Pico

Raspberry Pi Pico Basic V2

3
  •  (draf)ESP8266-01 WiFi with Raspberry Pi Pico
  • (draf)Raspberry Pi Pico with nRF24L01
  • (draf)ESP8266-01 WiFi DHT11 with Raspberry Pi Pico

Raspberry Pi Pico W

2
  • Connect to WIFI with Raspberry Pi Pico W
  • Langkah2 blink LED via WiFi on Pico W

Smart Car DIY

3
  • Smart Car Parking System
  • Balance Smart Car
  • Wifi Smart Car
View Categories
  • Home
  • Docs
  • Raspberry Pi Pico Basic
  • Control Servo SG90 Motor Dengan Raspberry Pi Pico

Control Servo SG90 Motor Dengan Raspberry Pi Pico

1 min read

Persiapan Bahan #

– Raspberry Pi Pico / Pico W
– Servo SG90
– Breadboard
– Kabel Jumper Male to Male
– Kabel Micro USD
– Aplikasi Thonny IDE

Wiring Servo SG90 Raspberry Pi Pico #

  • Connect the VCC to 3.3V
  • GND To GND
  • Signal Pin of the SG-90 Servo Motor to GP0 pin of the Raspberry Pi Pico.

Sample Code MicroPython Code/Program #

The code is divided into two parts. One is servo.py & the other is main.py. The servo.py is the library required by Servo Motor.

servo.py

from machine import Pin, PWM
 
 
class Servo:
    """ A simple class for controlling a 9g servo with the Raspberry Pi Pico.
    Attributes:
 
        minVal: An integer denoting the minimum duty value for the servo motor.
        maxVal: An integer denoting the maximum duty value for the servo motor.
 
    """
 
    def __init__(self, pin: int or Pin or PWM, minVal=2500, maxVal=7500):
        """ Creates a new Servo Object.
 
        args:
 
            pin (int or machine.Pin or machine.PWM): Either an integer denoting the number of the GPIO pin or an already constructed Pin or PWM object that is connected to the servo.
             minVal (int): Optional, denotes the minimum duty value to be used for this servo.
            maxVal (int): Optional, denotes the maximum duty value to be used for this servo.
 
        """
 
        if isinstance(pin, int):
            pin = Pin(pin, Pin.OUT)
        if isinstance(pin, Pin):
            self.__pwm = PWM(pin)
        if isinstance(pin, PWM):
            self.__pwm = pin
        self.__pwm.freq(50)
        self.minVal = minVal
        self.maxVal = maxVal
 
    def deinit(self):
        """ Deinitializes the underlying PWM object.
 
        """
        self.__pwm.deinit()
 
    def goto(self, value: int):
        """ Moves the servo to the specified position.
 
        args:
 
            value (int): The position to move to, represented by a value from 0 to 1024 (inclusive).
 
        """
        if value < 0:
            value = 0
        if value > 1024:
            value = 1024
        delta = self.maxVal-self.minVal
        target = int(self.minVal + ((value / 1024) * delta))
        self.__pwm.duty_u16(target)
 
    def middle(self):
        """ Moves the servo to the middle.
        """
        self.goto(512)
 
    def free(self):
        """ Allows the servo to be moved freely.
        """
        self.__pwm.duty_u16(0)

main.py

import utime
from servo import Servo
 
s1 = Servo(0)       # Servo pin is connected to GP0
 
def servo_Map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
 
def servo_Angle(angle):
    if angle < 0:
        angle = 0
    if angle > 180:
        angle = 180
    s1.goto(round(servo_Map(angle,0,180,0,1024))) # Convert range value to angle value
    
if __name__ == '__main__':
    while True:
        print("Turn left ...")
        for i in range(0,180,10):
            servo_Angle(i)
            utime.sleep(0.05)
        print("Turn right ...")
        for i in range(180,0,-10):
            servo_Angle(i)
            utime.sleep(0.05)
Updated on 11/01/2023

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
HC SR501 Pir Motion Sensor GerakKontrol Servo SG90 Dengan Potensio Di PI PICO

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Contents
  • Persiapan Bahan
    • Wiring Servo SG90 Raspberry Pi Pico
    • Sample Code MicroPython Code/Program

Contact Us

Selamat datang di TOKO ONLINE KHURSLABS

  • Alamat :
  • Jl. Lamongan Barat III No.52, Sampangan, Gajahmungkur, Semarang, Jawa Tengah, Indonesia, 50236
  • sales@khurslabs.com
  • | | +62 816-383640 | +62 811-383640
  • Jam Operasional
  • Senin - Jum'at : 08.00-16.00
  • Sabtu : 08.00-12.00
  • Minggu & Tanggal Merah : Libur
  • | | | | Toko Raspberry Pi

Products

  • Raspberry Pi 5 Computer
    Raspberry Pi 5 Single Board Computer Rp1.200.000 – Rp1.850.000
  • Hi-Link HLK-10M24 AC 220V to DC 24V 10 Watt 0.42A Hilink Power Supply
    Hi-Link HLK-10M24 AC 220V to DC 24V 10 Watt 0.42A Hilink Power Supply Rp55.000
Proteo - A free theme designed with by YITH

Social Chat is free, download and try it now here!