About sensors and microcontroller PIC18F4550

Thread Starter

Claudinho

Joined Nov 1, 2015
10
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.
 

MaxHeadRoom

Joined Jul 18, 2013
19,006
I assume they are analogue devices so the RA0 is AN0 but RA4 is not a analogue input AFAIK.
Max.
 

spinnaker

Joined Oct 29, 2009
7,815
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.
 

Thread Starter

Claudinho

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

MaxHeadRoom

Joined Jul 18, 2013
19,006
The 18f4550 manual has a section on it and gives an example.
Have you programmed a Pic before?
Max.
 

Thread Starter

Claudinho

Joined Nov 1, 2015
10
The 18f4550 manual has a section on it and gives an example.
Have you programmed a Pic before?
Max.
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?
 

spinnaker

Joined Oct 29, 2009
7,815

Here is some sample code to do analog input

Code:
#define PANEL_TRIS          TRISCbits.TRISC0
#define   PANEL_ANSEL_ANS_BIT     ANSELbits.ANS4

#define BATTERY_TRIS       TRISCbits.TRISC1
#define   BATTERY_ANSEL_ANS_BIT   ANSELbits.ANS5

void ADC_Init(void)
{
  
    
   PANEL_TRIS = 1;         //Configure as input
   PANEL_ANSEL_ANS_BIT = 1;   //Disable digital input

   BATTERY_TRIS = 1;       //Configure as input
   BATTERY_ANSEL_ANS_BIT = 1;   //Disable digital input
  
  ADCON2 = 0b10111000;
  
}

unsigned int ADC_Convert(unsigned char port)
{  
   unsigned long i;

   ADCON0 = (port <<2) | 1;

   ADCON0bits.GO_DONE = 1;  // start conversion
  while (ADCON0bits.GO_DONE == 1);  // wait for it to complete
   i = (ADRESH * 256);
   i = ADRESL + i;
  
   return i;
}
 

shteii01

Joined Feb 19, 2010
4,667
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)?
 

Thread Starter

Claudinho

Joined Nov 1, 2015
10
No you need to do an analog to digital conversion. You have analog sensors. They can only be read with an analog input.
Here is some sample code to do analog input

Code:
#define PANEL_TRIS          TRISCbits.TRISC0
#define   PANEL_ANSEL_ANS_BIT     ANSELbits.ANS4

#define BATTERY_TRIS       TRISCbits.TRISC1
#define   BATTERY_ANSEL_ANS_BIT   ANSELbits.ANS5

void ADC_Init(void)
{
  
    
   PANEL_TRIS = 1;         //Configure as input
   PANEL_ANSEL_ANS_BIT = 1;   //Disable digital input

   BATTERY_TRIS = 1;       //Configure as input
   BATTERY_ANSEL_ANS_BIT = 1;   //Disable digital input
  
  ADCON2 = 0b10111000;
  
}

unsigned int ADC_Convert(unsigned char port)
{  
   unsigned long i;

   ADCON0 = (port <<2) | 1;

   ADCON0bits.GO_DONE = 1;  // start conversion
  while (ADCON0bits.GO_DONE == 1);  // wait for it to complete
   i = (ADRESH * 256);
   i = ADRESL + i;
  
   return i;
}

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

Thread Starter

Claudinho

Joined Nov 1, 2015
10
Here is some sample code to do analog input

Code:
#define PANEL_TRIS          TRISCbits.TRISC0
#define   PANEL_ANSEL_ANS_BIT     ANSELbits.ANS4

#define BATTERY_TRIS       TRISCbits.TRISC1
#define   BATTERY_ANSEL_ANS_BIT   ANSELbits.ANS5

void ADC_Init(void)
{
  
    
   PANEL_TRIS = 1;         //Configure as input
   PANEL_ANSEL_ANS_BIT = 1;   //Disable digital input

   BATTERY_TRIS = 1;       //Configure as input
   BATTERY_ANSEL_ANS_BIT = 1;   //Disable digital input
  
  ADCON2 = 0b10111000;
  
}

unsigned int ADC_Convert(unsigned char port)
{  
   unsigned long i;

   ADCON0 = (port <<2) | 1;

   ADCON0bits.GO_DONE = 1;  // start conversion
  while (ADCON0bits.GO_DONE == 1);  // wait for it to complete
   i = (ADRESH * 256);
   i = ADRESL + i;
  
   return i;
}

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"?
 

spinnaker

Joined Oct 29, 2009
7,815
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.
 

Thread Starter

Claudinho

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

spinnaker

Joined Oct 29, 2009
7,815
And what have you done to troubleshoot the issue?

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

spinnaker

Joined Oct 29, 2009
7,815
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.
 

Thread Starter

Claudinho

Joined Nov 1, 2015
10
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.
 
Top