Monday, May 11, 2015

JAVA Digital Hall sensor with Raspberry Pi

Hello there,

Linear Hall sensor with comparator
All right this is the review on the next two lessons on linear hall sensors. Lesson 02 uses an analog linear hall sensor and an analog to digital conver IC ADC0832. The lesson 03 uses a linear hall sensor with added comparator, which allows some sensitivity tuning of the sensors by adjusting the potentiometer. The code for both lessons is the same, thus the review is all in one post.

Magnet and Linear Hall sensor
The fun part of this lesson is on getting familiar with "what on earth is analog to digital conversion ?". Grate resources on this matter are: Wikipedia article and Sparkfun article

For those of you that prefer to learn while watching awesome videos check this out Electronics 201: Analog / Digital Conversion.

In particular for this lesson we have the IC ADC0832CCN and the datasheet can be found here. The datasheet of the hall sensor can be useful as well for further hacks.  

The hole idea is about converting the voltage fluctuations (produced by a magnetic field) into a digital number value which represents the intensity of the magnetic field. The hall sensor generates the analog voltage variations and the ADC0832 encodes the numeric values that can be interpreted in the code. 

The original C code is correct, but rather difficult to understand and not optimal. For example the switch of pin ADC_DIO to an input mode is inside of the first for loop. This is unnecessary and confusing. And the ADC0832 initialization code has some extra steps not required and it is not easy  to understand. After a good reading of the datasheet I was able to understand what is going on in that part of the code.

The Java translated code for these two lessons can be found in the "Ex02_03_LinearHall" with the unit tests in "Ex02_03_LinearHallTest". The important part of the code is the function "get_ADC_Result()". This function will start the communication by setting the pin ADC_CS low, then is setting up the MUX address (which is a configuration byte) and then will start reading the first byte as Most Significant Bit MSB followed by the second byte as Least Significant Bit LSB and stop the communication by setting the CS line up again. Please check the code in Ex02_03_LinearHall for more details. The ACD0832 datasheet have of course full details about the communication protocol, and the key part to understand this communication code is the timing diagram for the ADC0832.



This is all for now. Here there is a nice video to prove that this all works perfectly fine in 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.

Thanks for reading, I hope you enjoyed it.

Posted by Marc Andreu.

No comments: