Tuesday, June 16, 2015

JAVA Mix sensors part II with Raspberry Pi

Hello there, 

Rotary encoder
Here it goes, few more exercises from the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". This post will be a quick review of the lessons 19, 20, 23, 24 and 25. These are a collection of simple sensors that work fine and with simple code. 

Lets start with lesson 19 which is about the Rotary Encoder sensor. The exercise Ex17_RotaryEncoder.java is a direct translation from the C code of the kit's lesson. However it is not really an efficient solution because of the continuous polling of the input pins, which will eat all the available resources of the small PI. In this previous post JAVA Rotray Encoder with Raspberry Pi, I implemented some variations with much more efficient ways of reading values from this sensor.   

Auto flashing LED
Moving on to lesson 20 with an auto flashing led. This is just a led which automatically changes colours like the ones in the Christmas tree. The code Ex18_AutoFlash.java is just setting the pin high. No much more to comment on this lesson. I see this kind of led can be good for some decorative project, but not sure what else could be useful for. Lesson 21 and 22 has been already reviewed in previous posts, because they deserved special mention so then this post can be short and easier to read. 

Obstacle avoidance
So, on the next lesson 23 with an obstacle avoidance sensor there are to variants of the exercise. The Ex21_Obstacle01.java is implemented as a listener on the input pin. This is a more efficient approach to read input signals for the PI. The other one Ex21_Obstacle02.java is a polling implementation, which is not that efficient from the point of view of CPU consumption but it is much more sensitive to detect obstacles. Over all both implementations work quite well.

Tracking sensor
Next, lesson 24 with a tracking sensor. Again the code Ex22_TrackSensor.java is simple and a direct translation from the C code of the original kit. It works well and its simple. Not much to comment on this sensor, just waiting to have time to build a tracking bot some day. :-)

Microphone sensor
And finally but not least the lesson 25 with two versions of microphone sensors. Basically they are both the same sensor with analog and digital outputs, just one is more sensitive than the other. The analog value can be read with the Analog to Digital converter ADC0832 used in many other previous exercises. And the digital signal works like a threshold value which can be adjusted with the variable potentiometer on the sensor. The exercise code is  Ex23_Microphone.java. What is pending to do for this exercise is to find the way to calculate the actual decibel values based on the analog readings of the ADC0832. Some day I will add this just for fun.

That is all for now, below there are few videos to show how the exercises should work if all works fine. If something is not working please leave me a comment below and I will do my best to help. 

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.

Thanks for reading,


Auto flashing LED



Obstacle avoidance



Tracking sensor



Microphone sensor


I hope it helps
Keep on hacking

Posted by Marc Andreu.

No comments: