About sensors and microcontroller PIC18F4550

Discussion in 'Embedded Systems and Microcontrollers' started by Claudinho, Nov 1, 2015.

  1. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    Hey

    I have a question in my course work and would like to ask for your help. I've made a homemade moisture sensor with two galvanized nails placed at a distance from one another and i'm going to use a NTC10K temperature sensor. I wonder how can I read the signal output of the two sensors. The humidity sensor is connected to RA0 pin PIC18F4550 and the temperature in the same microcontroller RA4.

    Thanks, guys.
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,571
    2,381
    I assume they are analogue devices so the RA0 is AN0 but RA4 is not a analogue input AFAIK.
    Max.
     
  3. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    Sorry, I didn't understand what you said.
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    I can't imagine how Max could be more clear. RA4 is not analog input. You are trying to read an analog sensor from it.
     
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,571
    2,381
    IIRC the 4550 has 13 analogue channels to pick from.
    Max.
     
  6. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    I need to do a digital analog conversion, right? could you tell me how I do it?
     
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,571
    2,381
    The 18f4550 manual has a section on it and gives an example.
    Have you programmed a Pic before?
    Max.
     
  8. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,399
    497
    Sensor datasheets?
     
  9. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    Yes, I have programmed PIC before. But as I am a student, still I don't know everything and have difficulty to understand digital analog conversion. I haven't learned it and am having trouble.
    If I did know this, I probably wouldn't be here, right?
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019

    No you need to do an analog to digital conversion. You have analog sensors. They can only be read with an analog input.
     
    Claudinho likes this.
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019

    Here is some sample code to do analog input

    Code (Text):
    1.  
    2.  
    3. #define PANEL_TRIS          TRISCbits.TRISC0
    4. #define   PANEL_ANSEL_ANS_BIT     ANSELbits.ANS4
    5.  
    6. #define BATTERY_TRIS       TRISCbits.TRISC1
    7. #define   BATTERY_ANSEL_ANS_BIT   ANSELbits.ANS5
    8.  
    9. void ADC_Init(void)
    10. {
    11.  
    12.    
    13.    PANEL_TRIS = 1;         //Configure as input
    14.    PANEL_ANSEL_ANS_BIT = 1;   //Disable digital input
    15.  
    16.    BATTERY_TRIS = 1;       //Configure as input
    17.    BATTERY_ANSEL_ANS_BIT = 1;   //Disable digital input
    18.  
    19.   ADCON2 = 0b10111000;
    20.  
    21. }
    22.  
    23. unsigned int ADC_Convert(unsigned char port)
    24. {  
    25.    unsigned long i;
    26.  
    27.    ADCON0 = (port <<2) | 1;
    28.  
    29.    ADCON0bits.GO_DONE = 1;  // start conversion
    30.   while (ADCON0bits.GO_DONE == 1);  // wait for it to complete
    31.    i = (ADRESH * 256);
    32.    i = ADRESL + i;
    33.  
    34.    return i;
    35. }
    36.  
    37.  
     
    Claudinho likes this.
  12. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    The temperature sensor is an NTC 10k. But the humidity sensor are galvanized nails. Only with them gave a variation from 0 to 910. To extend the signal, put an amplifier LM358.

    12191533_853514798100389_8341586426601552934_n.jpg
     
  13. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,399
    497
    Ok. So output of humidity sensor (galvanized nails?) is analog signal, it goes to the LM358. Output of LM358 is analog signal, it goes to the PIC. Since signal is analog, it needs to be digitized, you do it by feeding it to the PIC ADC. Which pins of your PIC function as Analog to Digital Converter (ADC)?
     
    Claudinho likes this.
  14. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0

    Thank you, i'll try to add this to my code and see what happend
     
  15. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0

    To put the RA0 pin on "ADC_Convert" to the PIC identify just write "ADC_Convert (0)"? And then I read the value for the variable "i"?
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    var x = ADC_Convert(0)

    After that call x would contain the value read.

    Note: the value returned is not the actual voltage but a value representing the voltage. You will need to calculate the actual voltage based on the max voltage expected and the number of bits of resolution of your analog input.

    If you have a 10 bit input then just calculate the number of volts per bit.
     
    Claudinho likes this.
  17. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    I'm using MPLAB IDE program and is not working. I include the library XC8 and also did not solve the problem.
     
  18. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    And what have you done to troubleshoot the issue?

    Do you really expect someone to be able to help when you provide zero information?
     
  19. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    You didn't even bother to mention what the issue is. "Not working" can mean a million things. It could take years if we just start guessing.
     
  20. Claudinho

    Thread Starter New Member

    Nov 1, 2015
    10
    0
    In fact, now I was referring to the code that you gave me before. As I said, I am using this program and is giving error in the code.
     
Loading...