Wednesday, June 17, 2015

JAVA Mix sensors part III with Raspberry Pi

Hello there, 

Here it goes, the last mix of lesson reviews of the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". The ultimate post will be about a mini project set up with few sensors, but that is material from the near future. For this post I will do a quick overview of the lessons 26 to 30. All the details about wiring the sensors and a little bit of definition of the sensors can be found in the kit's documentation.  Similarly to the last post these group of sensors are not complicated at all, the code is easy and straight forward. 

Metal touch sensor
Starting with the lesson 26 with a Metal Touch sensor. The JAVA code exercise Ex25_MetalTouchSensor.java is implemented as a simple listener for the digital output. The analog output could be read using the ADC0832 IC as we did many times in previous exercises. Thus the sensor does not have any special issue. It just works fine and easy as a simple touch switch. I did not play with the analog value in this exercise, however I expect it to be similar to the previous sensors where we can interpret the ADC value, as contact intensity level in this case.

Flame sensor
Moving on to the lesson 27 with a Flame sensor and the code at the  Ex24_FlameSensor.java. This as well has a digial and an analog outputs. This exercise also focused on the digital output using a polling implementation. It could be better to implement a listener but as this is a simple translation from the origial C code I followed the convention of keeping things as similar as possible to the original source. The sensor is quite sensitive and the distance to the flame can be adjusted with the variable potentiometer on the sensor.

Relay module
The lesson 28 with a simple Relay module and the code at Ex26_Relay.java has not much meat to sell. The relay goes on or off when the pin is set high or low. Useful module to be used and simple to code. 

Joy Stick module
The lesson 29 with a Joystick PS2 module and the code at the Ex27_JoyStickPS2.java is a bit more interesting. The sensor requires the ADC0832 to read the X values from channel 0 and the Y values from channel 1. The switch has its own pin output and works with a listener or just by checking the pin state. For the coordinates movements this exercise just detects the X and Y movements to the maximum and minimum level of each axis. For example the direction up movement is detected when Y value is equals to 0 and this is when the joy stick is at the upper end of the Y axis. Similarly the down movement is the bottom end of the Y axis and has value 255, because of the ADC is only 8 bit precision. The values in between are disregarded in this exercise. However it could be easy the improve the logic to interpret the middle positions just by reading the X and Y values between 0 and 255. 

MQ-2 Gas sensor
This idea of reading the intermediate values is also a to do item in my future list. Maybe I could use those values to modify the output colours of an RGB led or the speed of a motor. For now this exercise is simple and easy to understand how the module works. And the code is as similar as possible to the original C code of the kit's documentation.

And the last single module lesson of the kit is the lesson 30 with the MQ-2 Gas sensor. The code exercise is the Ex28_Mq2.java. This is difficult sensor to test unless I set on fire my room. I will keep this test as pending for a while :-) The sensor is wired with the all famous ADC0832 and the value would come to represent the intensity of smoke particles in the air.

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. 

Here there are few videos to demonstrate how the exercises should behave while runing.


Metal touch sensor



Flame sensor



Relay module



Joy stick module


That is all for now, thanks for reading.
Please leave me a comment if you need further help to run this exercises. 
I hope it helps, 
Keep on hacking,

Posted by Marc Andreu.

No comments: