I'm working on my college project on which I have to measure the temperature show it on 16x2 LCD and also to switch the cooling device according to the temperature, also I have to use pH Sensor and have to display its value on 16x2. Currently I'm using the code below for Temperature and switching, but it is not working properly. It shows the temperature on the screen but also some garbage value, and also not switching properly.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN 7
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
const int ledPin = 6;
void setup()
{
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin, OUTPUT);
}
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
lcd.print("Temp C ");
lcd.setCursor(6,0);
lcd.print(temp);
lcd.setCursor(0,1);
lcd.println("Humid % ");
lcd.setCursor(6,1);
lcd.print(Hum);
delay(1000);
if (temp <= 29)
digitalWrite(8, LOW);
else if (temp>30)
digitalWrite(8, HIGH);
}
I will divide my answer into two parts:
Hardware:
Please describe your hardware configuration in order to narrow down your problem: How are you switching the relay? Is it optically coupled? Are you using a transistor? If so, what type? Does your relay have reverse diode protection? (Do not connect your relay directly to an Arduino pin)
Code
2.1. This for-loop in your setup() is incorrect:
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin, OUTPUT);
}
You are indexing from 8 to 8, hence you don't need a for loop to declare a single pin as an output.
2.2. In general, you can assign your output pins to variables, or as a macro like you did for the DHT. Just be consistent. Additionally, your code needs general tidying up:
a . Start your code with all libraries calling
#include <Wire.h> // The Wire.h library is already called by the LiquidCrystal_I2C library, so you don't need to call it if you use the I2C one
#include <LCD.h> // You are using the LCD library...
#include <LiquidCrystal_I2C.h> // but also the I2C lcd library. You don't need both.
#include <dht.h> // Is this the latest version? Use Adafruit's
b. Try to group all your macros at the beginning. Declare pins or constants here:
// Using macros
#define DHT11_PIN 7
#define DigitalPin 8
#define DHT11_PIN 7
or using constants for pins. Choose one, not both:
// Group all your const int together, for the sake of clarity
const int DHT_PIN = 7;
const int DigitalPin = 8;
const int ledPin = 6;
c. I believe this constructor is not correct for the library you chose. Check the example from this library (assuming it is the same, which I believe it is)
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
try with this constructor, instead:
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
d. The setup() function needs some changes too:
void setup()
{
// You don't seem to be using the serial library. However, if you use it, it is
// preferable to start it first.
Serial.begin(9600);
// LCD configuration
lcd.begin(16, 2); // Always call the begin() method first
lcd.setBacklightPin(3,POSITIVE); // Then, use additional configuration methods.
lcd.setBacklight(HIGH);
lcd.clear();
// Once again, please check if you are using the right library.
// It might be the cause of the garbage you see.
// Initialize the type of pin
pinMode(DigitalPin, OUTPUT);
}
e. According to Adafruit's library (please see the examples), you are missing a Macro to declare the type of DHT sensor you are using:
#define DHTTYPE DHT11
f. In your main loop() you are reading values with some strange parenthesis:
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
which, according to the latest library, should be:
// The latest library does not have this prototype: int chk = DHT.read11(DHT11_PIN); What is it for in your dht lib?
float temp = DHT.readTemperature(); // instead of: float temp=(DHT.temperature); --> which should be DHT.temperature(); in any case
float hum = DHT.readHumidity(); // instead of: float Hum=(DHT.humidity);
TL;DR: Share your hardware in order to assist you. Update your libraries, clean-up your code, and use the right methods from the latest libraries.
Thankyou so much for your guidance, below is the working code for me:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 7 //digital pin for sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
pinMode(8, OUTPUT); //for configuring relay output
lcd.begin(16, 2);
dht.begin();
}
void loop() {
delay(1000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
lcd.print("NO CONNECTION");
return;
}
//Hum code
lcd.setCursor(0, 0);
lcd.print("Hum:");
lcd.print(h);
lcd.print(" % ");
//Temp code
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print(t);
lcd.print(" C");
//To operate relay
{
if (t <= 26)
digitalWrite(8, LOW);
else if (t>27)
digitalWrite(8, HIGH);
}
}
I am using an Attiny 9.
I connected a LED to PB1.
I wrote following code and upload it to the Attiny 9.
#define F_CPU 800000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define D0 PB1
int main(void)
{
power_adc_disable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
TCCR0A |= (1<<WGM00)|(1<<WGM01)|(1<<COM0B1);
TCCR0B |= (1<<WGM03)|(1<<WGM02)|(1<<CS00);
DDRB |= 1<<D0;
PORTB |= 1<<D0;
_delay_ms(3000);
PORTB &= ~(1<<D0);
for (uint8_t i = 255; 247 < i; --i)
{
OCR0B = i;
_delay_ms(70);
}
for (uint8_t i= 247; -1 < i; --i)
{
OCR0B = i;
_delay_ms(4);
}
while(1){
sleep_enable();
sleep_cpu();
}
return 0;
}
I want the LED to be lighten and darken. But after I uploaded the code, the LED illuminates continuously. I also read a datasheet of Attiny 9. I followed how to use PWM. But it's not working.
What's wrong?
There are a number of issues with that code.
You're putting the processor in to power down mode, which according to the datasheet, disables the clocks and only asynchronous events can take place.
You're setting it to use fast PWM mode with OCR0A as your top, but you aren't setting OCR0A anywhere. You won't generate any compare matches if you your timer can't count high enough. It would appear from your loops that you are expecting the timer to count to 255.
You only change the duty cycle of the PWM before your while loop, so even if your PWM gets set up correctly, you won't ever be able to see your LED pulse like you want.
Here's a link giving an example of how to set up a PWM and change its duty cycles:
http://allaboutavr.com/index.php/2017/05/13/chapter-5-timers-and-pwm-pulse-width-modulation/
Ty something like this:
#include <avr/io.h>
#include <util/delay.h>
#define D0 PB1
int main(void)
{
DDRB |= 1<<D0;
TCCR0A |= (1<<WGM00)|(1<<COM0B1);
TCCR0B |= (1<<WGM02)|(1<<CS00);
while (1) {
for (uint8_t i = 0; i < 255; i++) {
OCR0B = i;
_delay_ms(4);
}
for (uint8_t i = 255; i >= 0; i--) {
OCR0B = i;
_delay_ms(4);
}
}
}
I am trying to write a customized boot-loader for ATmega AVR's. I write a code, and it work perfectly in small AVR's like ATmega32A and ATmega8A. But when i want to use it in ATmega128A, it writes nothing in flash segment.
I'm sure Fuses are correct, ATmega103 mode is disabled, and the program starts at boot section, but it does nothing..
Before calling function "boot_program_page" I set PORTC and turn some LED's on, and after that I cleared PORTC and LED's goes off. so the code in executing completely too.
The function I am using is an example provided in avr/boot.h.
It should write some data ( 0x00 actually ) in page 0 of flash memory..
here is my code :
#define F_CPU 8000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/boot.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
void boot_program_page (uint32_t page, uint8_t *buf);
int main(void)
{
uint8_t data[SPM_PAGESIZE] = {0};
uint32_t page = 0;
DDRC = 255;
PORTC = 255;
page *= (uint32_t)SPM_PAGESIZE;
boot_program_page(page,data);
_delay_ms(1000);
PORTC = 0;
while (1)
{
}
}
void boot_program_page (uint32_t page, uint8_t *buf)
{
uint16_t i;
uint8_t sreg;
// Disable interrupts.
sreg = SREG;
cli();
eeprom_busy_wait ();
boot_page_erase (page);
boot_spm_busy_wait (); // Wait until the memory is erased.
for (i=0; i<SPM_PAGESIZE; i+=2)
{
// Set up little-endian word.
uint16_t w = *buf++;
w += (*buf++) << 8;
boot_page_fill (page + i, w);
}
boot_page_write (page); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
// Reenable RWW-section again. We need this if we want to jump back
// to the application after bootloading.
boot_rww_enable ();
// Re-enable interrupts (if they were ever enabled).
SREG = sreg;
}
i am a beginner to microcontroller and I just got into ADC but whenever I try to do a conversion it never works, I am trying to display the result on an LCD but the problem is not the LCD's because I tried its code alone and it worked so the problem is definitely the ADC's registers, here is the full code::
main.c:
#include "config.h"
int result;
void main(){
TRISAbits.TRISA0 = 1; //Set Port A for input
ADCON0 = 0b01000000; //Configuring ADCON0 register
ADCON1 = 0b10000000; //Configuring ADCON1 register
ADCON0bits.ADON = 1; //Turn on ADON bit in ADCON0 register to turn on ADC module
__delay_us(50); //Delay for the capacitor to be charged
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE == 1);
result = ADRESH && ADRESL;
initLCD();
write_character(result);
while(1);
}
config.h:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)
#define _XTAL_FREQ 4000000
void send_command(int command);
void write_character(int character);
void enable_blink(void);
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);
void initLCD(void);
void send_command(int command){
PORTCbits.RC0 = 0;
PORTB = command;
enable_blink();
}
void write_character(int character){
PORTCbits.RC0 = 1;
PORTB = character;
enable_blink();
}
void enable_blink(){
PORTCbits.RC1 = 1;
__delay_ms(10);
PORTCbits.RC1 = 0;
__delay_ms(10);
}
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
PORTCbits.RC0 = 0;
PORTBbits.RB7 = 1;
PORTBbits.RB6 = b6;
PORTBbits.RB5 = b5;
PORTBbits.RB4 = b4;
PORTBbits.RB3 = b3;
PORTBbits.RB2 = b2;
PORTBbits.RB1 = b1;
PORTBbits.RB0 = b0;
enable_blink();
}
void initLCD(){
TRISB = 0;
TRISC = 0;
send_command(0x38);
__delay_us(40);
send_command(0x01);
__delay_ms(1.75);
send_command(0x0C);
__delay_us(40);
}
You may have a problem with your result line. Try the following:
result = ( ( ADRESH << 8 ) | ( ADRESL ) )
The double & operator ( && ) is the logical AND. AND will return true ( 1 ) as long as ADRESH and ADRESL are both greater than 1. You want the bitwise OR ( | ) operator to combine two bytes into one int, with the ADRESH as the most-significant-byte and the ADRESL as the least-significant-byte.
Hopefully this works!
I have covered lately adc and lcd in microcontroller and gone into USART and as usual my first code I make is not working and I need some help with discovering the problem, here's the code:
Transmitter code:
main.c:
#include "config.h"
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 1;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
while(TRMT == 0);
if(PORTDbits.RD0 == 1){
TXREG = 0xFF;
}else{
TXREG = 0;
}
}
}
Receiver code:
main.c:
#include "config.h"
char recieve;
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 0;
PORTDbits.RD0 = 0;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
RCREG = recieve;
if(recieve == 0xFF){
PORTDbits.RD0 = 1;
}else{
PORTDbits.RD0 = 0;
}
}
}
and for both transmitter and receiver projects config.h is a header file where I set the frequency of crystal oscillator and configuration bits so it is the same file/code for both projects
config.h:
/*
* File: config.h
* Author: Fady
*
* Created on August 25, 2014, 1:53 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)
#define _XTAL_FREQ 4000000
EDIT
I am trying to communicate 2 pic microcontrollers in which if I press the button on D0 in transmitter it sends a 0xFF data and then the receiver checks for if the received data == 0xFF if yes it turns on the LED on D0 in receiver but when I connect it on isis the TX pin of receiver keeps blinking high and low (red and blue) each half a second but when I press the button the high (red) signal keeps for a bit longer but keeps flashing with high and low and I suppose this is for start bit but the receiver doesn't turn on the led that's the error part I don't know what's wrong here
Its just a very stupid silly c programming mistake I wrote:
RCREG = recieve;
which will take the value of receive and assign it to RCREG instead it should be
receive = RCREG;
which will assign the value of RCREG to receive and worked after building...