Friday, May 29, 2015

JAVA Switch sensors with Raspberry Pi

Hello there,
Passive buzzer
Here few more lesson reviews of the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". For some unknown reason there is not lesson 11 in the documentation so the reviews are for lessons 12, 13, 14, 15, 16 and 17. Lesson 12 is about buzzers and the other ones are different kinds of switch sensors, it means, sensors which can be used to switch circuits on and off. 

Active buzzer
Lesson 12 is a short simple exercise to get to know what are the buzzers (small speakers). Maybe for some manufacturing fault my active buzzer looks like is flipped and the connections to make it work are as follows: 

   GND ----------- 's' 
   GPIO0 --------- '-'

A part from the connections correction, the original C code works fine. However as I am in my mission to "JAVAtitation" of this kit using the Pi4j library, the code exercises in JAVA can be found at Ex12_ActiveBuzzer.java  and  Ex12_PassiveBuzzer.java .


Passive buzzer sensor


Button with laser sensor
Lesson 13 is a simple lesson to operate a button and capture the event. The event can be captured by polling the pin constantly (which is not ideal, because of the overuse of CPU resources) or by using an Interruption Service Rutine. Thus I implemented an event listener variation which works fine and achieves the same goal as using an ISR callback. The code for these two variation is here Ex13_Button.java  and Ex13_ButtonListener.java.



Button switch sensor


Lesson 14 is about a photo-interrupter sensor. It is quite interesting and very sensitive. It can be implemented to detect when the beam of light is broken or when is re-established. It works nicely and its simple to connect. The code of JAVA variations are Ex14_LightBreak.java and Ex14_LightBreakListener.java


Photo interrupter switch sensor

Tilt switch sensor
Lesson 15 is about Tilt-switch sensor. This sensor is not really or maybe I should say, not at all, sensitive. It maybe that the sensor is faulty. With a bit of tapping and shaking the ball inside gets to the right position to make the connection. The code comes in two flavours as well, like the button exercise. One for polling and one with a listener event. The JAVA code exercises are Ex15_TiltSwitch.java and Ex15_TiltSwitchListener.java.


Tilt switch sensor

Mercury switch sensor
Lesson 16 use a Mercury-switch sensor. The mercury switch is like a tilt switch but the mercury should absorb better the vibrations. Nonetheless the connection is not always properly established which results in missing some events. The JAVA code is being implemented with an event listener and for some strange reason like in lesson 11 the correspondent code exercise numbering does not follow the conventions thus the code is in Ex28_MercurySwitch.java.



Mercury switch sensor

And finally but not least lesson 17 is a sensor with a mercury switch and a LED. When the mercury switch is connected the LED pin is setted to high and the LED turns on. Just for fun I added a second LED to the same LED pin 01, thus when one LED is on the other is off. This makes a nice effect when moving the mercury switch sensor. See video below.


The code for this exercise is also very simple. There are as well two variations, one with polling and one with ISR. See Ex29_MagicCup.java and Ex29_MagicCupInt.java for more details.



Magic cup sensor


For full details about how to set up your Raspberry Pi and how to run these exercises please check out my old post Raspberri Pi with Pi4j and Junit testing mockups. Or leave me a comment if you have any issue or question.

That is all for now, I hope you enjoy it and it helps. 
Thanks for reading, 
Keep on hacking !

Posted by Marc Andreu.

No comments: