Introduction: Control Hall Sensor by Java

Picture of Control Hall Sensor by Java

The Hall effect of semiconductor is much better than the metal. Many Hall element is come out of this phenomenon and widely used in automatic industry technology, testing technology and information processing , etc.Hall effect is the basic method to study the performance of semiconductor. Hall coefficient was measured based on Hall effect experiment, and can judge the conduction type,carrier concentration, carrier mobility and other important parameters of semiconductor materials.

Step 1: Hardware List

3* Male to female DuPont

1* pcDuino

1* Linker Hall Sensor
Previous slide

Step 2: Wiring Diagram

Picture of Wiring Diagram

Hall Sensor                  pcDuino

VCC                                       3.3V

GND                                     GND

RX                                          one of D1-D13 must be the same as the code

Step 3: Test Code

GPIO control entity class
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class GPIO_Pin {

    private String modeURI = "/sys/devices/virtual/misc/gpio/mode/";
    private String statusURI = "/sys/devices/virtual/misc/gpio/pin/";
    private int pin = 0;
    public static final String HIGH = "1", LOW = "0", INPUT = "0", OUTPUT = "1", INPUT_PU = "8";

    public GPIO_Pin(int pin) {
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin = pin;
    }

    public GPIO_Pin(String pin) {
        // Finalize file paths
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin =Integer.parseInt(pin);
    }

    public int getPin() {
        return pin;
    }

    public void overrideURI(String uri) {
        modeURI = uri + "mode/gpio" + pin;
        statusURI = uri + "pin/gpio" + pin;
    }

    public void setMode(String mode) {
        writeToFile(getModeURI(), mode);
    }

    public void set(String state) {
        writeToFile(getStatusURI(), state);
    }

    public void setHIGH() {
        writeToFile(getStatusURI(), HIGH);
    }

    public void setLOW() {
        writeToFile(getStatusURI(), LOW);
    }

    public void setModeINPUT() {
        writeToFile(getModeURI(), INPUT);
    }

    public void setModeOUTPUT() {
        writeToFile(getModeURI(), OUTPUT);
    }

    public void setModeINPUT_PU() {
        writeToFile(getModeURI(), INPUT_PU);
    }

    public String getModeURI() {
        return modeURI;
    }

    public String getStatusURI() {
        return statusURI;
    }

    public String getPinMode() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getModeURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }

    public String getPinStatus() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getStatusURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }

    private void writeToFile(String URI, String data) {
        try {
            File file = new File(URI);
            file.delete();
            File newFile = new File(URI);
            newFile.createNewFile();
            FileWriter writer = new FileWriter(URI);
            writer.write(data);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        GPIO_Pin ledpin=new GPIO_Pin(1);
        ledpin.setModeOUTPUT();
        while (true) {
            ledpin.setHIGH();
            Thread.sleep(400);
            ledpin.setLOW();
            Thread.sleep(400);
        }
    }
}
Test class
package com.trc.gpio;

public class test {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        GPIO_Pin buttenpin=new GPIO_Pin(9);
        GPIO_Pin ledpin=new GPIO_Pin(8);
        buttenpin.setModeINPUT();
        ledpin.setModeOUTPUT();

        while (true) {
            System.out.println();
            //ledpin.setLOW();
            System.out.println(buttenpin.getPinMode());
            Thread.sleep(4);
            }
    }

}

Step 4: Test Result

Picture of Test Result

When there is a metal close, will have a different display.

Comments

Dustin Rogers (author)2014-03-06

Good question.

russ_hensel (author)2014-03-06

I am an old time programmer, my first computer had 1K of memory, more recently
used Arduino. I am used to working without dynamic memory allocation or garbage
collection, which Java does have ( I have worked with it too ).

So in your program you do things a lot like File file = new File(URI); which allocates
memory and then you leave the subroutine which causes “file” to be garbage collected.
This works the memory manager hard.

Is this your standard practice, or just because this is an illustration?

About This Instructable

999views

11favorites

License:

More by Yanny Guo:Use pcDuino as Photo Booth ControllerUse Si4703 FM Breakout Board on Arduino UnoUse SIM900 GPRS/GSM Shield on pcDuino
Add instructable to: