Thursday, June 18, 2015

JAVA Interactive Temperature mesurement system with Raspberry Pi

Hello there, this is it, last post of this series. 

This last post will be about the last two lessons 31 and 32 of the world wide famous "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". They are basically the same system with minor modifications. For full details about wiring the sensors, can be found as always in the original kit's documentation. It is easy and simple to follow that part so I wont reinvent the wheel here. Just leave me a comment below if you have any issues about wiring. 

Simple Tem System
Starting then with the lesson 31 we can see that it is a simple temperature system with a buzzer, a RGB LED and a DS18B20 temperature sensor. Al three sensors with the Raspberry Pi act as a temperature alarm system. When the room temperature is between the boundaries provided via program arguments, the LED is green and buzzer if off. When the temperature is below the minimum level the LED turns blue and the buzzer produces a slow beep and when the temperature is above the upper limit the LED turns red and the buzzer goes nuts. See the JAVA code version at the Ex29_TempMonitor.java. Easy right ?


Interactive Temperature System
All right then add some more fun to the mix and in lesson 32 we find the same temperature alarm system with a joy stick. The idea here is that instead of having fixed lower and upper temperature boundaries we can adjust those levels with the joy stick. Moving the stick up and down we can adjust the upper limit and moving the stick right to left we can adjust the lower limit. The code is not too complicated and we already have all the ingredients from the previous exercises for each module. The combined JAVA cocktail for this lesson is the Ex30_TempMonitor.java.


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.

 This is a video to show how it should look like when you run the exercise 30. 


 Interactive Temperature System

That is all for now,
Many thanks for reading,
I hope you like it and it is helpful,

Posted by Marc Andreu.

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.

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.

Monday, June 15, 2015

JAVA DS18B20 sensor with Raspberry Pi

Hello there

This is a quick review of the lesson 18 of the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". This lesson is about 1-wire devices and in particular we will be reading values from the DS18B20 temperature sensor. The documentation of the kit is quite enough to get a basic introduction about what it is the 1-wire communication.

DS1B20 sensor
However in January 2015 the Raspbian operating system was updated and it is required to change some configuration to detect these type of devices. This post from the REUK.co.uk  has full details of the hole process from installation to device detection.

In summary to solve the issue about detecting the device, it is just required to add the line "dtoverlay=w1-gpio" at the end of the file "/boot/config.txt". With this simple modification and after rebooting the Pi. The device should be detected and listed under the command: "ls -l /sys/bus/w1/devices/". A new device link should appear with the name "28-00000xxxxxx".

Just follow the original documentation of the kit or the article from REUK.co.uk to do a quick verification test. If everything is fine we can now run the lesson's exercise "Ex16_Ds18b20.java" with the following command:

    sh runPi.sh "org.mandfer.sunfunpi4j.Ex16_Ds18b20 28-00000xxxxxx"

Just replace "28-00000xxxxxx" with your device file name. This should print the current temperature on the console output like in the video below. 


Reading values from the DS18B20


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.

This is all for now, 
Thanks for reading and please post a comment if you have any issue or questions. 
Keep on hacking,


Posted by Marc Andreu.

Tuesday, June 09, 2015

JAVA Photoresistor sensor with Raspberry Pi

Hello there, one more little hack.

Keyes GL55 Photoresistor
This is a quick post on how to read values from a photo resistor sensor using a Raspberry Pi and Pi4j. This exercise is based on the lesson 21 of the the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". The kit's documentation can be found here. The original C code of the lesson works fine but I am not sure what the value represents. So I did my research to get real values which makes more sense. The sensor itself is a GL55 and the small PCB is configured as a voltage divider. The photo resistor itself is a 10K Ohms resistor and in the PCB there is another 10K resistor. In Roboticlab's sited there is a nice and very complete article which explains all the details of what is and how does a photo resistor work.

Wiring diagram for the ADC0832
Based on this article I was able to implement the code to calculate the different values which we can interpret from the sensor. We start by reading the voltage drop across the voltage divider with the help of an Analog to Digital Convertor IC. In this case I am using the ADC0832 which comes with the kit. It is only and 8-bit precision IC but it works well for learning and hacking purposes. From the voltage value we can calculate the resistance of the sensor and from the resistance value we can then calculate the luminance level in LUX units. Again, full details for all this calculations are very well explained in the Roboticlab's photoresistor article. And the code for this exercise can be found at Ex19_PhotoResistor.java.

Luminance Intensity ref. values
The only IMPORTANT thing that I had to sort out is the wiring. For the ADC0832 its easy, just follow the diagram above and connect the sensor's signal wire to the channel 0 (CH0). However the keyes sensor has bug. I had to swap the GND and VCC and connect VCC to +5V for the calculations to make sense and get luminance values that make sense on the reference table from the Roboticlab's article. 

With all this I got fairly good reading which one day will be wisely used for a nice little robotics project.


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.

Many thanks for reading, 
I hope it helps,
Keep hungry, keep foolish and keep on hacking ;-)

Posted by Marc Andreu.

Git help

Hello there

This is a quick post about few resources for git help. This is a short collection of links but very powerful ones :-)


Official git documentation:   http://git-scm.com/

Github related help:   https://help.github.com/

Tour guide with hands on git:   http://gitimmersion.com/

Quick git tips:   http://gitready.com/

Visual git cheat sheet:   http://ndpsoftware.com/git-cheatsheet.html

Please share in the comments if you know of any other nice git help resource. 

Many thanks for reading,
I hope it helps,

--
-------------------------------------
 Marc Andreu Fernàndez.
-------------------------------------

Monday, June 08, 2015

JAVA DHT11 sensor with Raspberry Pi

Hello there,

DHT11 sensor
This is the review on Lesson 22 DHT11 Sensor of the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". I have to admit that Java Native Interface is hard stuff. Even though with time and being stubborn enough you can do anything. I think the main issue with JNI is the lack of documentation. I may do a JNI tutorial later on. However for this exercise I created a JNI script which makes the process of building and installing the native library much easier.

I found a couple of good sources which mixed together made a nice simple cocktail. From the GreenHouse project I found a good example about how to use JNI with a small C code library. Their C code did not work with my sensor. I think the sensor they used might be slightly different because the initialization timings before the readings are a bit different from the ones that I had from the Sunfounder sensors kit C code 20_DTH11. So I just had to refactor the GreenHouse dhtreader.c code with the sunfounder's 20_DHT11. All what was left then was a little bit of polishing.

Sample readings
I researched about JNI and how to compile and install the library in the Raspberry Pi. I first found out that I had to use the Java arm openJdk 7 instead of the Oracle Jdk 7. Looks like the oracle one have no JNI capabilities. After that was sorted out and few hours of banging my head with the desk I came up with the script jniDHT11SensorReaderBuilder.sh which will compile and install the native library into the default location of the "java.library.path" property. There are many locations for this property, in this case I choosed /usr/lib. If you want to give it a try just check out the project into your Raspberry Pi with the command:

    git clone https://github.com/marcandreuf/sunfounder-sensors-raspi-4j.git

Then navigate to the project source folder "src/main/java" and if you have the Java OpenJdk 7 in the default location (check the script for more details) you should be able to run the script to install the native library with:

   sudo sh jniDHT11SensorReaderBuilder.sh


After few seconds the library should be installed and you all ready to run the Java class to test a single reading. Just connect the DHT11 sensor to GPIO01 and run the command:

   sudo java org.mandfer.dht11.DHT11SensorReader

You should get a simple reading of the temperature and humidity. If it is all fine and you want to check a better example which read values every 1.5 seconds try to run the exercise 20 from the project root folder with:

   sh runPi.sh org.mandfer.sunfunpi4j.Ex20_DHT11_Native

That is all for now, 
Thanks for reading. If you have any issue, leave me a comment below.



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.

Posted by Marc Andreu.

Tuesday, June 02, 2015

Pi4j button with ISR and debouncing

Hello there,

Switch button with LED indicator
I had a little bit of fun with the Interrupt Service Routine configuration in the Pi4J exercises that I am currently working on. I am translating from C code exercises into JAVA, just for fun, do not ask much more about the reasons ;-)

I found that an implementation of a button Interrupt Service Routine in Pi4j is not that straight forward as it is for a listener.

First I noticed that if the call back "GpioInterruptCallback" class is not an in line class, the call back wont work and some nasty exception will come from the WiringPi library. Lets say for example that the call back is defined as an inner class or an external class in some other file, it wont work. At least not for me, instead I had to put the callback class as an in line class like this:

    Gpio.wiringPiISR(btnPin, Gpio.INT_EDGE_FALLING, new GpioInterruptCallback() {
            ....
            @Override
            public void callback(int pin) {
                .....
            }
            ....
    });

Second for some other reason the declaration of the "Gpio.wiringPiISR(..)" needs to happen in an static method. For example the main() or some other static method. I ended up creating a static method like this:

    private static void gpioIsrSetup() {
        ..... 
        Gpio.pinMode(btnPin, Gpio.INPUT);
        Gpio.pullUpDnControl(btnPin, Gpio.PUD_UP);        
        Gpio.pinMode(ledPin, Gpio.OUTPUT);
        Gpio.pullUpDnControl(ledPin, Gpio.PUD_DOWN);
        Gpio.digitalWrite(ledPin, false);        
        Gpio.wiringPiISR(btnPin, Gpio.INT_EDGE_FALLING, new GpioInterruptCallback() {
        .....
   }

Finally I added some de-bouncing control software to eliminate the extra events caused by switch bouncing. I think it works quite well, but in summary the listener implementation alternative has a little bit better performance while doing repeated fast switching of the button. However for a normal one-off switch interaction this code works perfectly well. 

For more details about the code please see the exercise Ex13_ButtonISR.java and if you want to compare with the listener implementation take a look at the Ex13_ButtonListener.java.

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.

Many thanks for reading, 
Keep on hacking,

Posted by Marc Andreu.

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.

Saturday, May 23, 2015

JAVA Analog - Digital Thermistors with Raspberry Pi

Hello there,

One more lesson from the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". This one was a bit more interesting. Digging down into what is a Thermistor

Analogue Thermistor KY-013
This kit comes with two thermistor sensors, one is the module KY-013 and the other is a Thermistor Analog + Digital Electronic Temperature Sensor. There is a difference in the value generated if connected to 5V. For the formula that I used to calculate the ambient temperature I found it most suitable to use 3.3V.

Well here the fun was in getting an actual temperature value. The original C code from the files "analogTempSensor.c" and "analogTempSensor2.c" is working fine, however I still do not understand what the values represent. Neither in the code or in the documentation explains what is going on. All what this C code does is to read the value from the ADC0832 and substract 60 or 140. Well, as I said, I am still scratching my head around this code.

Thermistor analog + digital
So I decided to do a little bit of research to understand what is a thermistor and after a bit of testing I find out that the KY-013 module is rated as 10K resistor. The module acts as a voltage divider and the ADC0832 value will represent a voltage value between 0 and 255, due to it is an 8-bit IC Analog to Digital Converter. The key part to understand is that this value can be mathematically approximated to a temperature value by using the Steinhart-Hart equation.



Analog pin reading running Ex11_01

Based on a couple of forum discussions here and here and the NTC thermistor datasheet from Murata, I managed to get temperature readings quite close to my home thermometer. For full details on the mathematical calculations and values used in the Steinhart-Hart equation please see the code in the Ex11_01_AnalogTempSensor. Or write a comment below if you have any question and I will do my best to remember what I did.

This sensor is very sensitive so I let it run for a few second or minutes to test if the temperature reading was actually adjusting to variations. So a lot of window open and close events happen during testing ;-)

The Ex11_02_AnalogTempSensor is a simple implementation to detect the pin high or low value. The threshold is adjusted using the blue potentiometer. Running the first exercise code  Ex11_01_AnalogTempSensor is an easy way to adjust the potentiometer to the desired threshold value, because the temperature readings are printed on the console.


Digital pin reading running Ex11_02


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, I hope it helps
Thanks for reading
Keep on hacking

Posted by Marc Andreu.