Adding Console Port using RS232

From media_wiki
Jump to: navigation, search

ATmega328P Target Board Lesson 3
This Project we will setup ATmega328P Target Bd at 16 MHz and Flash the DHT11_AVRdude program to chip.
The program will turn on a red LED and then read a DHT11 Temp and humidity sensor. The program will process the data and send it out the RS232 port on the ATmega328P bd.Learn how to use

Prerequisites: Know how to use AVRdude with a USB programmer. (usbtiny, usbasp, etc.....)
avrdude -p m328p -c usbtiny -v | avrdude -p m328p -c usbasp -P usb -v
The Arduino IDE and Putty already installed. (any other serial program can be used)
  • Connect USBtiny to ATmega328P Target Board.
  • Test the connection to programmer: avrdude -p m328p -c usbtiny -v
  • Set Clock Fuse to 16 MHz avrdude -p m328p -c usbtiny -v -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFF:m

Connecting the DHT11 Temperature and Humidity Sensor

  • Connect DHT11 data pin to D2 on the ATmega328P Target bd. Pull up 4.7K resistor is optional for this project.
  • Connect +5 and Ground to the appropriate DHT11 pins.

Completed Breadbd DHT OLED.PNGRS232Pins.PNG
Connecting RS232 for the project

  • Connect PL2303TA USB to TTL Serial Cable Tx Green wire to the RX pin and the Rx White wire to TX pin on the ATmega328P Target bd
  • Don't forget to connect the Black wire to ground pin. Do not connect the Red wire to +5.
  • Use Device Manager to find out the comm port. This will be used later in the project
  • Open Arduino IDE and change the following settings: Under the Tools tab
      Board: ="Arduino/Genunio Uno"
      Programmer: = "USBtiny or USBasp"
      Comm Port = Device manager assigned Port number (optional step)
Library manager1.PNG
Add DHT.h Library using Arduino Library Manager
  • Copy-Paste/Load RS232ATmega328P.ino Arduino file. Flash the Program to the ATmega328P Target Board
/*RS232ATmega328P.ino
Program has the following functions:
Reads a DHT11 Temperature and Humidity sensor on Digital Pin 2,
Display the values out the RS232 port using Pins D0 and D1. Baud 9600 
Any temperature reading over 82 degrees will enable a negative output on digital pin 3
that will activate the relay.
Written by Rusty Cain Nov 4 2018. 
This program is a simple example. Any suggestions for improvements are greatly appreciated.
Please use and modify as appropriate.  */
#include "DHT.h"                             // DHT library
#define DHTTYPE DHT11                        // DHT 11
#define DHTPIN 2                             // what digital pin we're connected to
DHT dht(DHTPIN, DHTTYPE);
float t;                                      // Variable for temperature 
float h;                                      // Variable for Humidity 
int FanOn = 1;                                // 1 = FanOn - 0 = Fan Off
void setup(void) {
   pinMode(3, OUTPUT);                       // Set up FanOn Pin
   Serial.begin(9600);                       // Start commuication with serial console   
   FanOn = 0;                                // Fan Status 1 = On | 0 = Off
   Serial.println("Startup Please wait ");   // Print Start Banner
   Serial.println(" ");                      // Print Line space
   }
void loop(void)  {
   FanOn = 0;                                // Fan Status = Off
   Serial.println(" ");                      // Print Line space
   Serial.println("DMS Nov 2018 Workshop "); // Print Start Banner
   Serial.println("Using RS232 ATmega328P"); // Print Start Banner    
   Serial.println("Reading the DHT Sensor"); // Print Status Bannerto console port
   delay (1000);                             // Give Sensor time to read (min - 2sec)
   h = read_hum();                           // Read Humidity Sensor  
   t = read_temp();                          // Read Temperature Sensor  
     if ( t > 82.00 )                        // Check temperature value if over the limit turn on Fan
     {
       FanOn = 1;                            // Set Fan Status On = 1. 
       TurnFanOnFunction();                  // Call Function to turn on Fan
     }
   if ( t < 82.00 )                          // Check temperature value if under the limit turn Fan off 
     {
       FanOn = 0;                            // Set Fan Status = Off
       TurnFanOnFunction();                  // Call Function to turn off Fan
     }
  }
float read_temp() {
   float temp = dht.readTemperature(DHTPIN); // Read apin on DHT11
   Serial.print("Temp = ");                  // Send Value to console port
   Serial.print(temp);                       // Send Value to console port 
   Serial.println(" F ");                    // Send Value to console port
   return temp;                              // Return Value for evaluation  
 }
float read_hum() {
   float hum = dht.readHumidity(DHTPIN);     // Read apin on DHT11
   Serial.print("Humidity = ");              // Send Value to console port
   Serial.print(hum);                        // Send Value to console port 
   Serial.println("% ");                     // Send Value to console port 
   return hum;                               // Return Value for evaluation
 }
int TurnFanOnFunction()
 {
 if( FanOn == 0 ) 
 {
  digitalWrite(3, HIGH);                     // Turn Fan On
  Serial.println("DHTxx Fan is Off! ");      // Send Status of Fan to console port 
  Serial.print("FanOn Value = ");            // Send Value of FanOn to console port
  Serial.println(FanOn);                     // Send Status of Fan to console port 
  FanOn = 0;
 }
 if( FanOn == 1 )                            // FanOn =1 Fan be activated
 {
   digitalWrite(3, LOW);                     // Turn Fan On
   Serial.println("DHTxx Fan is On! ");      // Send Status of Fan to console port 
   Serial.print("FanOn Value = ");           // Send Value of FanOn to console port
   Serial.println(FanOn);                    // Send Value of FanOn to console port 
   delay (1);
 }
   return (0); 
 } 

Accessing your RS232 Comm Port
Using the PL2303TA USB to TTL Serial Cable Open the Comm Port using Putty.
Use the port number assigned by the Device Managerand set the baud rate to 9600.

You should now be able to see the output from the Target Board




AVRDUDE Class