Using Raspberry Pi Pico as Hid Device to control Mouse and Keyboard

For this all one needs is a rpi pico and circuit python for rpi pico.

The Circuit Python for pico can be found over here: https://circuitpython.org/board/raspberry_pi_pico/

Download the uf2 file and drag it over to the pico.

Next: download this as zip from or “git clone” it : https://github.com/adafruit/Adafruit_CircuitPython_HID

Extract it. In there , there is a folder called adafruit_hid , copy that folder to the pico where circuit python is already installed.

remember to transfer the python file with name “code.py” to pico, to run on boot.

This is the example for Mouse:

import time
import board
from digitalio import DigitalInOut, Direction, Pull
import time
import usb_hid
from adafruit_hid.mouse import Mouse
 
mouse = Mouse(usb_hid.devices)


x=0
y=0
mouse.move(x,y) 
# This will move the mouse from it's current position to x pixels 
# and y pixels. 
# So if the current position is (100,200) it'll move to
# (100+x,100+y)


This can also be used with a button so that it keeps the screen of pc awake ,like this:

import time
import board
from digitalio import DigitalInOut, Direction, Pull
import time
import usb_hid
from adafruit_hid.mouse import Mouse
 
mouse = Mouse(usb_hid.devices)
btn = DigitalInOut(board.GP14)
btn.direction = Direction.INPUT
btn.pull = Pull.DOWN
i=0
on=False
def onRelease():
    
    bState=btn.value
    released=False
    if (bState): 
        bState = btn.value
        while bState:
            bState = btn.value
    
        return True;


while True:
    
    if onRelease():
        on=not on
    if on:
        mouse.move(5,5)
        time.sleep(0.1)
        mouse.move(-5,-5)
        time.sleep(0.1)
    time.sleep(0.1)
    
    

This is the example for Keyboard.

import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

key_A=Keycode.A
key_Shift = Keycode.SHIFT
keyboard=Keyboard(usb_hid.devices)

time.sleep(2)
keyboard.press(key_Shift, key_A)
keyboard.release(key_Shift, key_A)

if you have any questions, email me at:

Prototype Of a mechanical servo Clock.

What is it?

Most Digital clocks are made up of 4 7-segment display. A seven segment display is 7 leds arranged in specific manner that when some light up it forms a number ranging from 0-9 or letters from A-F.

Seven-segment display - Wikipedia

What the machincal clock does is used servo motors as an led. so when you see the carboard cutout from top it replicates led_on and when looked from side looks like led_off.

Parts Needed

I used :

  • Arduino Uno
  • !6 bit channel Servo Controller.
  • Carboard Cut hexagon shapes.

Math behind the Cardboard Cutouts.

I initially thought that cutting the hexagon cutouts was easy. In fact it was quite challenging. I had to find a perfect ratio.

  1. cut a rectangle with ratio 1:3 . I used 3:9 cm.
  2. draw a horizontal line across of length(1/2 of the short side ) from top and bottom of the long side. (Line T and B)
  3. Draw a verticle line from shor side to the other in the middle. ( center U and D)
  4. Connect U with edges Of T and D with B. then cutout thoses triangles to form a hexagon.
The diagram of one Hexagon. Recommended size is 3:9 cm but as long as the ratio is 1:3 the hexagons will fit perfectly

The code.


#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // this is the 'maximum' pulse length count (out of 4096)

bool down=false;
uint8_t servonum = 0;
int ang[7];
void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  setNum(1,8);
delay(1000);
}
void servo(int servoNum, int angleToMove){
  
  pwm.setPWM(servoNum, 0, angleToPulse(angleToMove) );
  }
void setNum(int digit, int numToSet){
  if(digit==1){
    switch(numToSet){
      case 0:
        if(!down){
        down=true;
        open();
        servo(1,180);
        close();
        }
        else{servo(1,180);}
        servo(2,90);
        servo(3,90);
        servo(4,90);
        servo(5,90);
        servo(6,90);
        servo(7,90);
        break;
        
      case 1:
        if(!down){
        down=true;
        open();
        servo(1,180);
        close();
        }
        else{servo(1,180);}
        servo(2,0);
        servo(3,0);
        servo(4,0);
        servo(5,90);
        servo(6,90);
        servo(0,0);
        break;
      case 2:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,90);
        servo(0,90);
        servo(5,0);
         
        servo(3,0);
        servo(6,90);

        break;
      case 3:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,0);
        servo(0,90);
        servo(5,90);
         
        servo(3,0);
        servo(6,90);
        break;
      case 4:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,0); 
        servo(4,0);
        servo(0,0);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,90);
        break;
      case 5:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,0);
        servo(0,90);
        servo(5,90);
         
        servo(3,90);
        servo(6,180);

        break;
      case 6:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,90);
        servo(0,90);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,180);
        break;
      case 7:
        if(!down){
        down=true;
        open();
        servo(1,180);
        close();
        }
        else{servo(1,180);}
        servo(2,90);
        servo(3,0);
        servo(4,0);
        servo(5,90);
        servo(6,90);
        servo(0,0);
        break;
      case 8:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}       
        servo(2,90); 
        servo(4,90);
        servo(0,90);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,90);

        break;
      case 9:
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,00);
        servo(0,90);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,90);
        break;
      
      case 10: //A
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,90);
        servo(0,0);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,90);

        break; 
       
      case 11: //B
        
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,0); 
        servo(4,90);
        servo(0,90);
        servo(5,90);
        servo(1,90); 
        servo(3,90);
        servo(6,180);

        break;
      
      case 12: //C
        if(!down){
        down=true;
        open();
        servo(1,180);
        close();
        }
        else{servo(1,90);}        
        servo(2,90); 
        servo(4,90);
        servo(0,90);
        servo(5,0); 
        servo(3,90);
        servo(6,180);

        break;
        
      case 13: //D
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,0); 
        servo(4,90);
        servo(0,90);
        servo(5,90);
        servo(1,90); 
        servo(3,0);
        servo(6,90);

        break;

      case 14: //e
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,90);
        servo(0,90);
        servo(5,0);
        servo(1,90); 
        servo(3,90);
        servo(6,180);

        break;

      case 15: //f
        if(down){
        down=false;
        open();
        servo(1,90);
        close();
        }
        else{servo(1,90);}
        servo(2,90); 
        servo(4,90);
        servo(0,0);
        servo(5,0);
        servo(1,90); 
        servo(3,90);
        servo(6,180);

        break;
        
      }
    }
  }
void open(){
  servo(4, 45);
  servo(5, 45);
  delay(20);
  }
void close(){
  delay(30);
  servo(4, 90);
  servo(5, 90);
  
  }
void loop() {
  
for( int i =0; i<16;i++){
setNum(1, i);
delay(1000);
}


}

int angleToPulse(int ang){
   return map(ang,0, 180, SERVOMIN,SERVOMAX);
}

I updated the code.

It has added funcionality for Letters A-F and Safer for a cutout to not overlap the other.

Until then goodbye!

How I accidently made a Robot Frog

I’ve always wanted to make a Robot dog or a spider ( I did make the spider eventually) but one day I wanted to make a robot dog, I only had 4 servos at that time and it was practicaly impossible to make a robot dog out of it, because we need 8 servos for balance and 4 servos for turning (12DOF) , I think it can also be done in 4 for balance and 4 for turning, but I never tried, back to the story, I was determined to take up a challenge and I started thinking negatively, that it was impossible, but eventually I reached somewhere.

At first i though this is not a dog, but it turned out I had made a Frog, I dicided to name it MiMo. It can only walk forward because I didn’t add any servos for turning due to a major problem.

the servos I used were made for carrying light Objects and not for robots, the arduino uno was very light and it worked perfectly with an external supply. But when I attached a battery (9v was necessary), MiMo would not be able to even move, so I dumped the Arduino Uno(size of a credit card) and used an Arduino nano( size of a pendrive), but it still was too heavy, I tried changing the batteries to smaller ones but it did not work. The only option was to get a LiPo battery but the small size, light-weight and 9v was too costly, so I gave on the idea and salvages the parts for other usefull projects.

My 2nd Robot N-A-R

N-A-R stands for Not A Robot, but it is a robot, that I made. It is an 8-DOF robot, i.e. 8 degrees of freedom, so it can move in 8 different ways. Image DOFs as joints in our body. When we move our elbow, we are using one of many DOF in our body, similarly the robot uses Servos for joints and as i have used 8 servos, N-A-R has 8DOF.I also made a post about X-O-R, which is a 17 DOF robot, which of course can move in 17 different ways, both of them were challenging, because X-O-R was big and heavy but was easy to make it walk due to it’s 17 DOF, but N-A-R was harder, even though it was smaller, I had very limited option/ degress of freedoms, it was certainly easier to balance than X-O-R but this took a lot of thinking.

If i went to explain everything we would be here all day, so that is a post for another time, until then GoodBye 🙂 .

Using an ultraSonic sensor with Arduino

ULTRASONIC SENSOR

Using sound to calculate short distance in a pretty good way of measuring distance, though it may not be a 100% accurate and cannot be fully depended on, it is a fun project to make and understand Sound.

The HC-SR04 ultrasonic sensor is most commonly used in projects. Ulrasonic sensors have 2 speakers, trigger (trig) and echo, the trig produces sound, and echo listens to it, the sound in on an ultrasonic level aka Ultrasound which cannot be heard to the human ear, the frequency of ultrasound is about 2-200 MHz (mega heartz) whereas a human can hear only 2000-5000 Hz.

The sensor can be controlled/ used with a micro-controller. when the ‘trig’ speaker is turned on it produces a sound wave, when the wave hits an object it is reflected, when the sound his the ‘echo’ speaker, the duration the wave took to travel from ‘trig’ to ‘echo’ is send to the micro-controller, in our case an Arduino Uno. The duration( in microseconds) can be converted to inches or centimetres. The following image explains everything.

Connecting and using with Arduino.

The circuit diagram for the connections is given below, it can be used with almost all mainstream Arduinos.


#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

The code above can be uploaded to an Arduino can can be used easily.

Arduino Cube Companion

So today I made my first Android App And I am soon going to publish it. It is a rubik’s cube companion. It is easy-to-use, and fairly simple. It has many options like a StopWatch, Timer (to Set a goal and beat your PB) . It also has a tutorial. It is not that great but good for beginners. The main Feature is Compatiblity with Arduino. You can use an UltraSonic Sensor And HC-06 or HC-05 with Arduino. Here are the schematics.

So the ultrasonic can be used as a Trigger for the timer on the app. Here is the code…

const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;
long dis;
int state=0;
long getCm(){long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   cm = (duration/29)/2;
   delay(100);
   return cm;}
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
//Serial.println(getCm());
dis = getCm();
//Serial.println(dis);
if(dis<=17 && state ==0){ // You Might want to change the 17 (dis) to more or less depending on your 
                           // needs.
dis = getCm();
  do{
 dis = getCm();
 if(dis>17){
  state = 1;
  Serial.println(state);
  break;
  }  
    }while(dis<=17);
    
}
else if(dis<=17 && state ==1){
    state = 0;
    Serial.println(state);
    dis = getCm();
    while(dis<=17){dis = getCm();}
  }
}

On the Phone click Dev-StopWatch or Arduino button. And then press Connect, Choose your bluetooth module connected to Arduino and Enjoy 🙂 ….

Design a site like this with WordPress.com
Get started