Shop OBEX P1 Docs P2 Docs Learn Events
Measure Voltage, output through p0-p7 — Parallax Forums

Measure Voltage, output through p0-p7

adiadi Posts: 7
edited 2008-01-24 23:09 in General Discussion
I have problem with measuring voltage and showing the result per pin 0-7. the Voltage that i measured range from 0V until 2,5V.

pin 3 is always 5V, even when i give no input voltage.( ADCValue is about 125)
and when the ADCvalue is about 79, pin 1 is 5 V and also pin 3.

Anyone knows why?confused:

here is my program:

import stamp.core.*;

public class Distanz {

static int PORTA;

final static char CLS = '\u0010';
static int ADCValue;
static ADC voltMeasurer = new ADC(CPU.pin14, CPU.pin15);


public static void main() {

CPU.setOutput (CPU.pins[noparse][[/noparse]0] + CPU.pins+CPU.pins+CPU.pins+CPU.pins+CPU.pins+CPU.pins[noparse][[/noparse]6]+CPU.pins[noparse][[/noparse]7]);

while(true){
CPU.delay(5000);
ADCValue = voltMeasurer.value();
System.out.print(CLS);
System.out.println("ADC value is: ");
System.out.println(ADCValue);
if(ADCValue<=64)
{
CPU.writePin(CPU.pins,true);
CPU.writePin(CPU.pins,false);
}
else if((ADCValue>64) && (ADCValue<=128))
{
CPU.writePin(CPU.pins,true);
CPU.writePin(CPU.pins,false);
}
else
{
CPU.writePin(CPU.pins,false);
CPU.writePin(CPU.pins,false);
}

} // end while
} // end main
}

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Adi Prasojo
Student of Mechatronics
Koblenz-Germany

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-22 11:09
    First, you must use the bitwise OR in setOutput

    CPU.setOutput (CPU.pin0 | CPU.pin1 | CPU.pin2 | CPU.pin3 | CPU.pin4 | CPU.pin5 | CPU.pin6 | CPU.pin7);

    Secondly, you must specify which pins in the if/else construct

    CPU.writePin(CPU.pin0,true);
    CPU.writePin(CPU.pin1,false);



    You can use pins[noparse][[/noparse]x] but then you must specify x. If the pin number is constant, using pinX
    requires less code.

    regards peter

  • adiadi Posts: 7
    edited 2008-01-23 20:13
    thanks peter, and sorry for the writing.

    actually all i want to do is to measure a voltage from a sharp distance sensor GP2D120(output vary fom 0V until 2,5 V). and i would like to show the result in PORTA. do you or anyone know about this?

    because I've tried to change the code just like you suggested. still I have the same problem(the output in Pin2 and 3 won't react according to the code).

    to measure the voltage i use pin16 and pin15.

    regards Adi

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Adi Prasojo
    Student of Mechatronics
    Koblenz-Germany
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-24 00:31
    Post your complete program as an attachement,
    maybe something else is wrong with your code.

    regards peter
  • adiadi Posts: 7
    edited 2008-01-24 05:33
    here is the code:

    import stamp.core.*;

    public class Distanz2 {

    static int PORTA;

    final static char CLS = '\u0010';
    static int ADCValue;
    static ADC voltMeasurer = new ADC(CPU.pin14, CPU.pin15);


    public static void main() {

    CPU.setOutput (CPU.pin0 | CPU.pin1 | CPU.pin2 | CPU.pin3 | CPU.pin4 | CPU.pin5 | CPU.pin6 | CPU.pin7);

    while(true)
    {
    CPU.delay(5000);
    ADCValue = voltMeasurer.value();
    System.out.print(CLS);
    System.out.println("ADC value is: ");
    System.out.println(ADCValue);
    CPU.writePort(PORTA,(byte)ADCValue);


    } // end while
    } // end main
    }

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Adi Prasojo
    Student of Mechatronics
    Koblenz-Germany
  • adiadi Posts: 7
    edited 2008-01-24 05:48
    i've changed the code, already.

    i've tried to make it a bit simple. but it doesn't work too. the output (from System.out.println(ADCValue)) vary from 70 to 125. I had never been able to get output 0 when the sensor output voltage 0V.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Adi Prasojo
    Student of Mechatronics
    Koblenz-Germany

    Post Edited (adi) : 1/24/2008 5:59:34 AM GMT
  • adiadi Posts: 7
    edited 2008-01-24 05:57
    inpVoltage(sensor)
    v
    |
    |
    res(22K)
    |
    +
    <p15
    |
    +---res(22K)---->p14
    |
    __+
    ---1uF
    |
    |
    |
    GND

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Adi Prasojo
    Student of Mechatronics
    Koblenz-Germany
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-24 06:34
    The ADC definition:

    · /**
    ·· * Create a sigma-delta analog to digital converter virtual peripheral and
    ·· * start it running.
    ·· *
    ·· * @param inPin the pin to be used as an input by the converter.
    ·· * @param outPin the pin to be used as an output by the converter.
    ·· */
    · public ADC(int inPin, int outPin) {
    ··· this.inPin = inPin;
    ··· this.outPin = outPin;
    ··· start();
    · }

    You use in your code:
    static ADC voltMeasurer = new ADC(CPU.pin14, CPU.pin15);
    so pin14 is the input pin, pin15 is the output pin.
    Your schematic shows them swapped.
    So try this in your code:
    static ADC voltMeasurer = new ADC(CPU.pin15, CPU.pin14);

    regards peter
  • adiadi Posts: 7
    edited 2008-01-24 23:09
    I've found the problem measuring the voltage from this GP2D120 sensor.
    it looks like his output must be pulled down. I've used 10 kohm resistor to pull it down.
    maybe if I use bigger pulled down resistor, would be better.

    The schematic is attached. hop.gif

    regards Adi

    this is the code:

    // Program Listing 4.3 - ADC Test

    import stamp.core.*;

    public class Distanz2 {

    //static int PORTA;

    final static char CLS = '\u0010';
    static int ADCValue;
    static ADC voltMeasurer = new ADC(CPU.pin9, CPU.pin8);


    public static void main() {

    CPU.setOutput (CPU.pin0 + CPU.pin1 + CPU.pin2 + CPU.pin3 + CPU.pin4 + CPU.pin5 + CPU.pin6 + CPU.pin7);

    while(true)
    {
    CPU.delay(5000);
    ADCValue = voltMeasurer.value();
    System.out.print(CLS);
    System.out.println("ADC value is: ");
    System.out.println(ADCValue);
    CPU.writePort(CPU.PORTA,(byte)ADCValue);


    } // end while
    } // end main
    } // end class declaration

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Adi Prasojo
    Student of Mechatronics
    Koblenz-Germany
    483 x 252 - 21K
Sign In or Register to comment.