USB Host Shield for Arduino

USB Host Shield for Arduino to Monitor USB data


Sparkfun has USB host shield that can work with Arduino.
I think this is great for the user who want to monitor USB data during the prototyping phase.
It is also great for my latency test project that I posted previously.
It seems like this is original done by Circuits@Home and it is super great work!
This post will focus on the setup of the tool and hopefully I can use this for other projects that I am working on.

Setup
Software preparation:
Launch the Arduino IDE.
There is library available through the library manager.
To launch the Library Manager, follow this.

Sketch > Include Library > Manage Libraries...

In the Library Manager, there is a search bar at the upper right.
Type "USB host shield" and you will find the library by Oleg from Circuit@Home.
In my case "USB Host Shield Library 2.0", and here is the screenshot  after the installation.










After the installation, restart the Arduino IDE and you can now refer to the example codes using the library from "File" menu.

File > Examples > USB Host Shield Library 2.0

So many examples here!

Hardware preparation:
Before you use this board, you need to solder the header pins so that you can stack the board on top of the Arduino.
Once you connect the board to Arduino, make sure the power switch is ON position and you will see the red LED is on. 


Test

For my test, I just used USB mouse.
Connect a USB mouse to the USB connector on the shield board.
From the Arduino IDE, select the exmaples.
I used USB HID descriptor example.

File > Examples > USB Host Shield Library 2.0 > HID > USBHID_desc

Upload the code to the Arduino.
Then, open the "serial monitor" from the Arduino IDE (small icon at the upper right).
You will see the data read from USB mouse as you move the mouse.




















Done!



                  

Raspberry PI USB Keyboard/Mouse Latency Test

USB Keyboard/Mouse Latency Test Using Raspberry Pi


Latency, latency, latency...
People ask those questions and I am also one of the guy who is interested in the latency...
From the USB test here and GPIO test here I have done before, I now decided to do quick latency test of keyboard or mouse.
In this test, I do the simple button press test and check how quickly the raspberry pi detect the USB input event.


Setup
Connect GPIO input to an external button:
I was too lazy to open up the unit and wire the button signal directly to the raspberry pi... sorry for the people who are interested in this post...
so I decided to wire a tact switch to the raspberry pi.
Then attach the switch to the one of the keyboard button.
  
Same as my previous GPIO test post, I am using GPIO20(pin #38) as interrupt input.
Also same as previous USB device input test post, I am using wireless keyboard with touchpad.
For this test, I used left button of the touchpad.


Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
Example:
import RPi.GPIO as GPIO
import time
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              global start
              start=time.time()
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
              for event in dev.read_loop():
                           if event.code == 272 and event.value ==1:
                                       end=time.time()
                                       elapsed=(end-start)*1000
                                       print(str(elapsed) + " ms")
except KeyboardInterrupt:
              GPIO.cleanup()

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py

Result

The result was not that great...
why?
Because the my setup was too lazy to measure...
The contact of external button and touchpad button do not happen at the same time.
It is totally up to how you press button.
In my case, I needed to press a bit strong so that both external button and touchpad button were pressed nearly at the same time...
Anyway, the number I got was around 8-10 ms.
Old logitech whitepaper says around 10ms depending on the environment, so maybe result is reasonable for this simple test??
"The typical latency of an advanced 2.4 GHz device operating in a clean environment is below 10 ms. In a noisy environment, this latency may increase depending on the strength, type and occurrence of the interference. "
This is wireless keyboard with touchpad so wired mouse/keyboard would be faster I would assume.

Done!



                  

Raspberry PI GPIO Test

GPIO Test Using Raspberry Pi


This post will talk about how fast raspberry pi can trigger GPIO. 
I did not want to investigate it deeply here but just wanted to quickly check it.
So, I decided to use the well maintained python GPIO library for raspberry pi.
For this test, I wired the output pin to input pin and measure the loopback time using timers.


Setup
Connect GPIO pins:
In my case, using GPIO16(pin #36) as output and GPIO20(pin #38) as input.
I used female jumper wires to connect those pins.
GPIO pin numbering diagram

Notes:


The GPIO software module for python is installed by default if you are using Raspbian.
If you want to double check the versions and/or install the latest version, refer to this site.


                                 


Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
Example:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              end=time.time()
              elapsed=(end-start)*1000
              print(str(elapsed) + " ms")
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
              while True:
                           GPIO.output(16, 1)
                           time.sleep(0.5)
                           start=time.time()
                           GPIO.output(16, 0)
                           time.sleep(0.5)
except KeyboardInterrupt:
              GPIO.cleanup()

Notes:
There are 2 types of mode settings.
GPIO.BOARD means it is referring to the pins by the number of the pin.
GPIO.BCM means it is referring to the pins by the "Broadcom SOC channel" number.
For the details, you can refer to this great article here.

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py

Result

The result was quite interesting.
It was around 0.2ms.
I think this is not a problem for the projects I do.

Done!



                  

Raspberry Pi USB Input Device Data Monitor (Part 1)

USB Mouse/Keyboard Data Monitoring Using Raspberry Pi


I am working on getting the raw USB mouse or keyboard data using raspberry pi.
To start with, I just decided to grab the data from the Logitech K400R wireless keyboard that I use for my raspberry pi.
This keyboard has the touchpad and size is good, not too small, not too large, and price is very reasonable. 

Logitech Wireless Touch Keyboard K400 with Built-In Multi-Touch Touchpad



Setup
Install pip:



$ sudo apt-get install python-pip python-dev build-essential 
$ sudo pip install --upgrade pip 
$ sudo pip install --upgrade virtualenv 
Install evdev module:

$ sudo pip install evdev
Check input events:

$ ls /dev/input
If you do not know which event is mouse or keyboard, follow this.
Install input-utils:

$ sudo apt-get install input-utils
$ lsinput



Python Code

This is the python example code.
Example 1:
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
for event in dev.read_loop():
             print(event)
Example 2:
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
print(dev)
for event in dev.read_loop():
             if event.type == ecodes.EV_KEY:
                        print(event)
Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py
Press any buttons on the keyboard or mouse and you will see the outputs in the terminal.
Done!



                  

Raspberry Pi Servo Motor Controller

DC Servo Controller Shield of Raspberry Pi

I found Adafruit motor controller shield for Raspberry Pi.
This shield supports up to 16 channel PWM for DC servo motor.
I do not know if I need all 16 channels, but it seems like very easy to use and can work for many servos and other PWM applications like LEDs.
Anyway, I decided to get one and play with it.

Setup
The setup is so easy, especially with all the instructions explained in the product page here.
I pretty much followed this page for all the setups.
For the servo motor, I used tilt and pan servo system with SG-90 from TowerPro.
The spec of this servo is:

  • Size : 23x11x29 mm
  • Voltage : 3V to 6V DC
  • Weight: 9g / 0.32oz
  • Speed : 0.12 sec/60 (at 4.8V)
  • Torque : 1.6 kg-cm

1. Assembly
When I received the package, all the header pins were not soldered, so I need to solder those header pins.
I did not mount capacitor this time but the product page explains about the "good to start" values which is n * 100uF where n is the number of servos if you are driving many servo or a lot of current.

2. Power
I used DC 5V 2A  power adapter and as I mentioned above, I did not mount any capacitors on the board.


2. Connection

Servo has 3 wires which is power, ground, and control signal (PWM).
In my case, orange, red, and brown, and those are signal, power, ground, respectively.
The board has silk screen with S, V+, and G and as you can probably guess this means Signal, Power, and Ground, respectively.
I connected the pan and tilt servo wires to channel 0 and 1 of my shield board.


                  

3. Testing
This shield board uses I2C for the communication. 
First of all, I would recommend to check if raspberry pi can recognize the I2C slave device on the shield board.

If I2C is not enabled on your raspberry pi, you can follow this tutorial. 
For this test, I used "i2c-tools" package and here is how to install and use it.

Install python-smbus and i2c-tools:
sudo apt-get install python-smbus 
$ sudo apt-get install i2c-tools
Run i2c test command:
$ sudo i2cdetect -y 1

If you see 0x40 (default I2C address of the shield board) in the output table, that means you are good to go.


Python code for testing
I used python to control the servo since there is very useful python code examples from Adafruit.
You can find all the information here.
This is my python example code using my servo system.
    *** Depending on the python version, you may get different result and my python version is 2.7.13.

Example:
#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time

pwm = PWM(0x40)
#pwm = PWM(0x40, debug=True)

servo0Mid = 360
servo1Mid = 450

pwm.setPWMFreq(60)
pwm.setPWM(1, 0, servo1Mid)
time.sleep(1)
pwm.setPWM(0, 0, servo0Mid)
time.sleep(1)

Crate a python script file as below and copy&paste the above code.
nano servo-test.py
Run the python script.
python servo-test.py
You will see the servo will move and stay at around the center position.
Done!

[AKM Chip Booster] Audio ADC AK5704 PCB Design

Designing a PCB prototype with AK5704 is not so difficult and I show an example with my design. People who are not familiar with AK5704,...