Skip to main content

Macropad using Raspberrypi Pico and Circuit Python



A budget-friendly DIY macro pad or a replacement for the Elgato stream deck.

Components Required

  1. Cherry MX Switches
  2. Raspberry Pi Pico
  3. LED- the color of your choice
  4. Copper clad and other related items to make PCB board
  5. Other miscellaneous items
Circuit Diagram

PCB and Circuit Diagram via EasyEDA Project Link

https://oshwlab.com/anurag_mehta/macropad-pcb

Code:

import time
import digitalio
import board
import usb_hid
import rotaryio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
keyboard = Keyboard(usb_hid.devices)
write_text = KeyboardLayoutUS(keyboard)
button = digitalio.DigitalInOut(board.GP15)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
encoder = rotaryio.IncrementalEncoder(board.GP10, board.GP11)
cc = ConsumerControl(usb_hid.devices)
button_state = None
last_position = encoder.position
btn1_pin = board.GP1
btn1 = digitalio.DigitalInOut(btn1_pin)
btn1.direction = digitalio.Direction.INPUT
btn1.pull = digitalio.Pull.UP.DOWN
btn2_pin = board.GP2
btn2 = digitalio.DigitalInOut(btn2_pin)
btn2.direction = digitalio.Direction.INPUT
btn2.pull = digitalio.Pull.UP.DOWN
btn3_pin = board.GP3
btn3 = digitalio.DigitalInOut(btn3_pin)
btn3.direction = digitalio.Direction.INPUT
btn3.pull = digitalio.Pull.UP.DOWN
btn4_pin = board.GP7
btn4 = digitalio.DigitalInOut(btn4_pin)
btn4.direction = digitalio.Direction.INPUT
btn4.pull = digitalio.Pull.UP.DOWN
btn5_pin = board.GP8
btn5 = digitalio.DigitalInOut(btn5_pin)
btn5.direction = digitalio.Direction.INPUT
btn5.pull = digitalio.Pull.UP.DOWN
btn6_pin = board.GP9
btn6 = digitalio.DigitalInOut(btn6_pin)
btn6.direction = digitalio.Direction.INPUT
btn6.pull = digitalio.Pull.UP.DOWN
btn7_pin = board.GP12
btn7 = digitalio.DigitalInOut(btn7_pin)
btn7.direction = digitalio.Direction.INPUT
btn7.pull = digitalio.Pull.UP.DOWN
btn8_pin = board.GP13
btn8 = digitalio.DigitalInOut(btn8_pin)
btn8.direction = digitalio.Direction.INPUT
btn8.pull = digitalio.Pull.UP.DOWN
btn9_pin = board.GP14
btn9 = digitalio.DigitalInOut(btn9_pin)
btn9.direction = digitalio.Direction.INPUT
btn9.pull = digitalio.Pull.UP.DOWN
while True:
    led.value = True
    
    current_position = encoder.position
    position_change = current_position - last_position
    if position_change > 0:
        for _ in range(position_change):
            cc.send(ConsumerControlCode.VOLUME_INCREMENT)
        print(current_position)
    elif position_change < 0:
        for _ in range(-position_change):
            cc.send(ConsumerControlCode.VOLUME_DECREMENT)
        print(current_position)
    last_position = current_position
    if not button.value and button_state is None:
        button_state = "pressed"
    if button.value and button_state == "pressed":
        print("Button pressed.")
        cc.send(ConsumerControlCode.PLAY_PAUSE)
        button_state = None
    
    if btn1.value:
        print("button 1 pressed")
        keyboard.send(Keycode.COMMAND,Keycode.Q)
        time.sleep(0.1)
        
    if btn2.value:
        print("button 2 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Notion')
        time.sleep(0.1)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn3.value:
        print("button 3 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Google Calendar')
        time.sleep(0.2)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn4.value:
        print("button 4 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Google Chrome')
        time.sleep(0.2)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn5.value:
        print("button 5 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Youtube')
        time.sleep(0.1)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn6.value:
        print("button 6 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('obs')
        time.sleep(0.1)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn7.value:
        print("button 7 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Salesforce')
        time.sleep(0.2)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn8.value:
        print("button 8 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Gmail')
        time.sleep(0.2)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
        
    if btn9.value:
        print("button 9 pressed")
        keyboard.send(Keycode.GUI, Keycode.SPACEBAR)
        time.sleep(0.1)
        write_text.write('Slack')
        time.sleep(0.1)
        keyboard.send(Keycode.RETURN)
        time.sleep(0.1)
time.sleep(0.1)


Reference

Comments

Popular posts from this blog

Automatic Fish Feeder using Raspberry Pi Pico

Introduction We are going to build an automatic fish feeder which can feed the fish every 24 hours. It will have a knob to control the food portion. By default, it will feed the fish every day at 8AM but there will be 2 buttons to set the custom time. Also, there will be a display, which will show details like set time, portion size, real-time, and temperature. The device runs on a 9V power supply. Required Components Raspberry Pi Pico 3D Printed Funnel 9V Motor 5V Relay RTC Clock Display Module Potentiometer Push Button LM 7805 LM 7809 9V Power Adapter DC power Female Adapter Capacitor Resistor Vero Board Soldering Tools Acrylic Sheet Other random parts and components Circuit Diagram Code from machine import I2C, Pin from urtc import DS1307 from ssd1306 import SSD1306_I2C import utime import machine import time counter=-1 motor=Pin(2, Pin.OUT) led=Pin(25,Pin.OUT) push_button = Pin(3, Pin.IN) push_button1 = Pin(5, Pin.IN) push_button2 = Pin(4, Pin.IN) analog_value1 = machine.ADC(28) a...