Flex sensor readings fluctuating largely

Thread Starter

tanshviii

Joined Sep 20, 2021
3
hello everybody

i am working on my college project using PIC16F876a

We're using a flex sensor to control the brightness of LED wireless through RF module 433 Mhz

The transmission of data from transmitter to receiver has no flaws as seen in the oscilloscope.

But once the flex sensor comes in to the picture, I'm experiencing fluctuations in its reading( in bent position) .

I understand that noise can cause that but , When inspected on logic analyser , readings of fluctuation reach even the 0 value which seems suspicious to me.

Flex is an a Vtg divider circuit with 100K resistor, given to ADC of first uC, LED is connected to PWM pin of second uC

I'm a beginner in this would appreciate any help possible

I'm attaching my codes a below for reference



transmitter code :

C:
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#include<xc.h>
#define SBIT_TXEN 5
#define SBIT_SPEN 7
#define SBIT_CREN 4
#define SBIT_ADON 0

#define SBIT_CHS0 3

#define _XTAL_FREQ 11059200 //We are running on 11.0592MHz crystal

void ADC_Initialize()
{
ADCON0 = 0b10000001; //ADC ON and Fosc/64 is selected & channel 0 selected
ADCON1 = 0b01000000; // Internal reference voltage is selected
}


int ADC_Read(int adcChannel)

{

ADCON0 = (1<<SBIT_ADON) | (adcChannel<SBIT_CHS0); //select required channel and turn ON adc



__delay_ms(200); //Acquisition Time(Wait for Charge Hold Capacitor to get charged )



GO=1; // Start ADC conversion

while(GO_DONE==1); // Wait for the conversion to complete

// GO_DONE bit will be cleared once conversion is complete
return(ADRESL); // return right justified 10-bit result

}




void UART_Init(int baudRate)
{
TRISC=0x80; // Configure Rx pin as input and Tx as output
TXSTA=(1<<SBIT_TXEN); // Asynchronous mode, 8-bit data & enable transmitter
RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN); // Enable Serial Port and 8-bit continuous receive
SPBRG = (11059200/(64*baudRate))-1; // baud rate

}
void UART_TxChar(char ch)
{
while(TXIF==0); // Wait till the transmitter register becomes empty
TXIF=0; // Clear transmitter flag
TXREG=ch; // load the char to be transmitted into transmit reg
}
char UART_RxChar()
{
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
}

void main() {
ADC_Initialize();
UART_Init(9600);
while(1)
{
UART_TxChar(ADC_Read(0));
__delay_ms(200);
}



}

Receiver code:

C:
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#include<xc.h>
#define SBIT_TXEN 5
#define SBIT_SPEN 7
#define SBIT_CREN 4

#define _XTAL_FREQ 11059200 //We are running on 11.0592MHz crystal

void UART_Init(int baudRate)
{
TRISC=0x80; // Configure Rx pin as input and Tx as output
TXSTA=(1<<SBIT_TXEN); // Asynchronous mode, 8-bit data & enable transmitter
RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN); // Enable Serial Port and 8-bit continuous receive
SPBRG = (11059200/(64*baudRate))-1; // baud rate @20Mhz Clock

}
void UART_TxChar(char ch)
{
while(TXIF==0); // Wait till the transmitter register becomes empty
TXIF=0; // Clear transmitter flag
TXREG=ch; // load the char to be transmitted into transmit reg
}
char UART_RxChar()
{
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
}
void main()
{
UART_Init(9600);
TRISC = 0X00; //Configure PORTC as output
CCP1CON = 0X0F; // Select the PWM Mode
PR2 = 0xF9; //Set the Cycle time for varying the duty cycle
CCPR1L = 50; // By default set the dutyCycle to 50
TMR2ON = 1; //Start the Timer for PWM generation
while(1)
{
CCPR1L=UART_RxChar();
__delay_ms(100);


}
}
 
Last edited by a moderator:

John P

Joined Oct 14, 2008
1,917
Well, two easy tests. Connect a fixed voltage source (could be just a resistor divider) in place of the flex sensor. If the noise is the same, then it's evidently not being caused by the sensor. Second, send the output to a computer instead of the radio link. If the noise is the same, it must be coming from the sensor and processing electronics, not the radio link.

The point is, whenever you have a mysterious problem, try to isolate it to one part of your system. Find ways to narrow it down until you can be sure that you know where trouble is occurring. Then decide whether there's a way to make an easy fix, or whether you have to design something completely new.
 

Thread Starter

tanshviii

Joined Sep 20, 2021
3
Well, two easy tests. Connect a fixed voltage source (could be just a resistor divider) in place of the flex sensor. If the noise is the same, then it's evidently not being caused by the sensor. Second, send the output to a computer instead of the radio link. If the noise is the same, it must be coming from the sensor and processing electronics, not the radio link.

The point is, whenever you have a mysterious problem, try to isolate it to one part of your system. Find ways to narrow it down until you can be sure that you know where trouble is occurring. Then decide whether there's a way to make an easy fix, or whether you have to design something completely new.
Thanks John I'll try doing that
 

trebla

Joined Jun 29, 2019
470
Try to format ADC data before sending it out, get for example 8 ADC readings and then calculate average and send the average value to far end. ADC readings tend to change not only due the noise but also due intermediate bit values during conversion.
 
Top