Friday, September 16, 2022

SW520 Mouse - Gesture controller part 2

For an index to all my stories click this text

In a previous story I showed you how to build a simple gesture controller with the SW520D sensors. You can re-read that story here:
http://lucstechblog.blogspot.com/2019/11/an-easy-gesture-controller.html

In this story I am going to build something practical with this gesture controller. I am going to show you how you can use it as a mouse for your computer.

The loyal readers of this weblog might remember that I made a similar gesture controller which I called a lightmouse. To use that sensor you would need to move your complete arm. And with this version you only need to move your hand. You can re-read the lightmouse story here: http://lucstechblog.blogspot.com/2017/12/lightmouse.html

Hardware.

The breadboard setup is basically the same as the setup in the previous story about this gesture controller. The difference is that we need to connect it to the USB-port of a computer.

To achieve communication with your computer you need to use an Arduino Leonardo or an Arduino Pro Micro. These two have a real USB controller on board and not just a serial over USB like the original Arduino has. I use an Arduino Pro Micro clone which I bought for a few dollar from my favorite Chinese supplier.

Next we need to connect the Arduino Pro Micro to the gesture board.




The four tilt switches are connected to digital IO port D6 D7 D8 and D9 of the Arduino pro Micro. Thats all. Do not alter the rest of the breadboard because the leds help you see if the movements are right.

The software

The software is straightforward as all the USB handling is done by the Arduino Pro Micro. The Arduino language has a Mouse.move command which as the name suggests simulates a mouse movement. The Mouse.press and Mouse.release commands simulate a mouse press. We are going to use these commands in the program.


/*SW520 MOUSE
  Luc Volders

Decides on which way the mouse will go by checking
which SW520 makes contact and which does not

using 4 SW520 put in a square form
top:    D6
Bottom: D7
Left:   D8
Right:   D9
*/

int dig1, dig2, dig3, dig4;

void setup()
{
  Mouse.begin();
  Serial.begin(115200);
}

void loop() 
  {
  
  dig1 = digitalRead(6);
  dig2 = digitalRead(7);
  dig3 = digitalRead(8);
  dig4 = digitalRead(9);

  Serial.print("UP ");
  Serial.println(dig1);
  Serial.print("Down ");
  Serial.println(dig2);
  Serial.print("LEFT ");
  Serial.println(dig3);
  Serial.print("RIGHT ");
  Serial.println(dig4);

    if (dig1 == 0)
    {
    //going up
    Mouse.move(0, -10, 0);
    Mouse.press(MOUSE_LEFT);
    delay(50);
    }

    if (dig2 == 0)
    {
    //going down
    Mouse.move(0, 10, 0);
    }

    if (dig3 == 0)
    {
    //going left
    Mouse.move(10, 0, 0);
    }

    if (dig4 == 0)
    {
    //going right
    Mouse.move(-10, 0, 0);
    }

  delay(70);
  Mouse.release(MOUSE_LEFT);
}

Let me explain the important parts of the program.


int dig1, dig2, dig3, dig4;

This line declares some variables which will be used in the program.


void setup()
{
  Mouse.begin();
  Serial.begin(115200);
}

First the Mouse.begin() command activates the USB port as a mouse.
Next the Serial port is activated which we will use as a textual feedback on the movements we make.


  dig1 = digitalRead(6);
  dig2 = digitalRead(7);
  dig3 = digitalRead(8);
  dig4 = digitalRead(9);

  Serial.print("UP ");
  Serial.println(dig1);
  Serial.print("Down ");
  Serial.println(dig2);
  Serial.print("LEFT ");
  Serial.println(dig3);
  Serial.print("RIGHT ");
  Serial.println(dig4);

In the loop the digital IO ports are read. And the results are printed in the Serial monitor. This way you can check if everything works as it should.


    if (dig1 == 0)
    {
    //going up
    Mouse.move(0, -10, 0);
    Mouse.press(MOUSE_LEFT);
    delay(50);
    }

This part checks which of the gesture controllers outputs was sending a signal. If it was dig1 then the detected movement was up. So we move the mouse 10 pixels up. At the same time we simulate pressing the left mousebutton by the command Mouse.press(MOUSE_LEFT);

So when the gesture controller points upwards the mouse moves up and at the same time the mousebutton is pressed. This way you do not need to put a switch on your fingers to simulate the mousebutton. Everything is done by hand gestures.

The tests for the other movements are similar.

The Mouse.move() command uses 3 parameters. Mouse.move(x, y, wheel). The x controls the horizontal direction, the y the vertical direction and the wheel parameter simulates a mousewheel. My program only uses the x and y parameters.


  delay(70);
  Mouse.release(MOUSE_LEFT);

The delay at the end makes sure the movement of the gesture controller is debounced. Mouse.release(MOUSE_LEFT); releases the mouse button.

As you can see the program is easily to adapt to your own needs.

Real world use.



The video shows you how the controller responds in a real world situation. I opened Paint in Windows and used the controller to choose colors and draw a straight line. A simple demonstration of what is possible.

Next time I show you how to build a mechanical maze game.

Till then, have fun !!!

Luc Volders

Friday, September 9, 2022

An easy gesture controller

For an index to all my stories click this text

As you might know by now I wrote, up to now, two books. A book about the ESP32 and a book about the Raspberry Pi Pico. You can find links to these books at the bottom of this page.

In these books I described a tilt sensor called the SW-520D. Lets first look at how it functions inside.



As you can see the way this sensor works is fairly straightforward. On the inside on one side of the sesor there are 2 contacts. And there are 2 metal balls inside. When the sensor is in the upright position the balls shorten the contacts. At an angle the balls still shorten the contacts. When horizontally the balls might shorten the contacts or the contacts are open. And when the sensor is at a down angle there certainly will be no contacts shortened.

You can use this sensor as a switch. You could use it for example as a means to test wether a box is upright or upside down.

Then I had an idea. How about using this sensor to control all kinds of appliences and even as a remote controller for a car. To achieve this you'll need multiple SW-520D's arranged in a particular way. I'll show you the fundamental setup here:





I'll bet you get the idea. Put this on a breadboard and lift the breadboard at an angle downwards and the top sensor will make contact. Lift the breadboard upwards and the bottom sensor will make contact etc etc etc.

So let's put this on a breadboard and attach some leds so the sensors to check wether this works.




The above breadboard setup shows you how to attach all items. The only thing you need to supply externally is power. I use a breadboard connector for a USB power supply or powerbank but you could attach batteries.
Just make sure you put the SW520's in the right way on the breadboard. Mind where the sensor makes the contacts and put the it the opposite way on the opposite sites.




Here is an example on how this looks in real life.




And here a short movie that demonstrates how this functions. As I am filming and controlling the setup at the same time it is not the best demo but it gives you a good hint on how this works in real life.

When I build this I immediately had some ideas and I will work them out and show them in future stories. In the mean time I will give you a real example here.

Gesture controlled MP3 player.

Remember the MP3 player I described in this article:
https://lucstechblog.blogspot.com/2019/05/mp3-player-stand-alone.html

Well what I did is to replace the buttons with two SW520D tilt sensors.




Re-read the above mentioned story about this MP3 player and adjust the breadboard like this. Now you can control jumping to the next or previous number on the SD card by a quick forward or backwards nod. Keeping the board in an upward or downward angle will set the volume louder or softer.

The setup with 4 sensors is ideal for building some gesture controlled projects. And I will be devellopping them for future stories. So keep on coming back.

Till next time
Have fun

Luc Volders

Friday, September 2, 2022

Pico W in Cirkit

For an index to all my stories click this text

As you might notice I am slowly moving away from Fritzing to Cirkit Designer for my schematics. One of the reasons is that Cirkit designer has a far easier way to design your own components. And of course it is free !!!

You can find Cirkit designer here: https://www.cirkitstudio.com/index.html

And as you also might have noticed is that there is a new Raspberry Pi Pico available. The Pico W with Wifi !!! So I designed a new component in Cirkit designer.



To use this component just download the file presented below in Cirkit designer and then save it on your own system.

https://www.mediafire.com/file/nlm7nlyhywyxpa8/PicoW.ckt/file

Next step is to alter the breadboard layout to your own liking. Add buttons or sensors etc. and save the new design to your own computer. That's it.

After a short discussion with the designers of Cirkit they offered to add a function so that you do not have to save the complete project but you can save just the new component.
This would be great. Then we could start a database with custom components !! So keep looking at the updates for Cirkit Designer.



Just a few short Pico-W observations. 


As you can see I made a sloppy job of soldering the headers. But my soldering Iron just broke down and I had to use an old one with a worn out tip. Make sure that you use a good tip so you don't get any short circuits between the pins.

The easiest way to solder the header pins to the Pico-W is to place the Pico-W with headers on a breadboard and then solder them. Just as discussed here:

http://lucstechblog.blogspot.com/2021/02/raspberry-pico-first-look.html


The Pico-W pins are compatible with the original Pico's pins so the pin layout-aid is still valid. You can find it here: http://lucstechblog.blogspot.com/2021/03/raspberry-pico-pin-layout-help.html

And also the reset button for the Pico can be used on the Pico-W. You can find that story here:

http://lucstechblog.blogspot.com/2021/02/raspberry-pico-rest-button.html

That's all for now.
Have fun

Luc Volders