# About sensors and microcontroller PIC18F4550

#### 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.

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

#### Claudinho

Joined Nov 1, 2015
10
Sorry, I didn't understand what you said.

#### spinnaker

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

Joined Jul 18, 2013
23,142
IIRC the 4550 has 13 analogue channels to pick from.
Max.

#### Claudinho

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

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

#### shteii01

Joined Feb 19, 2010
4,644
Sensor datasheets?

#### 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,835
I need to do a digital analog conversion, right? could you tell me how I do it?

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

#### spinnaker

Joined Oct 29, 2009
7,835

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

{

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

}

{
unsigned long i;

ADCON0 = (port <<2) | 1;

ADCON0bits.GO_DONE = 1;  // start conversion
while (ADCON0bits.GO_DONE == 1);  // wait for it to complete

return i;
}

#### Claudinho

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

#### Attachments

• 566.3 KB Views: 1

#### shteii01

Joined Feb 19, 2010
4,644
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

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.

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

{

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

}

{
unsigned long i;

ADCON0 = (port <<2) | 1;

ADCON0bits.GO_DONE = 1;  // start conversion
while (ADCON0bits.GO_DONE == 1);  // wait for it to complete

return i;
}

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

#### Claudinho

Joined Nov 1, 2015
10

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

{

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

}

{
unsigned long i;

ADCON0 = (port <<2) | 1;

ADCON0bits.GO_DONE = 1;  // start conversion
while (ADCON0bits.GO_DONE == 1);  // wait for it to complete

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,835

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

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,835
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,835
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.