Why Build a Portable Weather Station with PIC Microcontrollers?

Building a portable weather station with PIC microcontrollers combines embedded programming, sensor integration, and mechanical design into a single hands-on project. It teaches how to read analog and digital sensors, manage power for field operation, and display data on a small screen—all without relying on a full-fledged single-board computer like a Raspberry Pi. A PIC-based station uses fewer components, draws less power, and gives you complete control over the firmware. You can pack the final device into a Pelican case or a 3D‑printed enclosure and take it hiking, camping, or use it as a classroom demonstration tool.

This expanded guide builds on the basic instructions to cover sensor selection, wiring details, programming in C (with MPLAB X and the XC8 compiler), calibration, and advanced features such as Bluetooth logging and solar charging. By the end, you will have a reliable weather station that can run for days on a single battery charge.

Expanded Component List

Choose parts that are widely available, well‑documented, and easily work with 5 V or 3.3 V logic. The table below summarises the key items. Prices are approximate (USD) and may vary.

ComponentRecommended PartInterfaceNotes
MicrocontrollerPIC16F877A or PIC18F45K22GPIO, ADC, I²C, UARTPIC16F877A is a classic; PIC18F45K22 offers more memory and a 3.3 V option.
Temperature sensorLM35 (analog) or DS18B20 (digital, 1‑Wire)Analog / 1‑WireLM35 outputs 10 mV/°C; DS18B20 gives ±0.5 °C accuracy.
Humidity sensorDHT22 / AM2302Single‑wire digitalWorks from 0–100% RH, ±2% accuracy. Slower than I²C but reliable.
Barometric pressureBMP180 or BMP280I²CBMP280 also provides temperature. Use with 3.3 V regulator if using PIC16F877A.
Display1.3″ OLED 128×64 (SSD1306) or 16×2 LCDI²C or parallelOLED draws less power; I²C version uses two pins.
Battery3.7 V Li‑ion (18650) + 5 V boost converterChoose capacity ≥ 2000 mAh. Add protection circuit.
Wireless module (optional)HC‑05 Bluetooth or ESP‑01 (UART Wi‑Fi)UARTHC‑05 connects to a phone; ESP‑01 can post data to the cloud.
EnclosureABS plastic box or 3D‑printed caseInclude vents for sensors; use silicone sealant for weatherproofing.

Important: The PIC16F877A runs at 5 V, while the BMP180 and OLED (I²C) are often 3.3 V devices. Use a level shifter (e.g., a simple MOSFET circuit) or a 3.3 V regulator for the sensor bus. Many modern PICs (PIC18FxxK42) operate at 3.3 V and simplify interfacing.

Detailed Assembly Instructions

A neat, modular build helps debugging and future upgrades. Work on a solderless breadboard first, then transfer to a perfboard or PCB when the circuit is stable.

Power Supply Block

Goal: Deliver a stable 5 V to the PIC and sensors (or 3.3 V if using a 3.3 V PIC).

  • Connect the 18650 battery (3.7 V) to a low‑dropout 5 V boost converter module (e.g., MT3608‑based).
  • Add a 100 µF electrolytic capacitor at the boost converter output to smooth ripple.
  • For the 3.3 V rail, use a linear regulator (AMS1117‑3.3) after the 5 V output. This keeps the analog readings stable.
  • Include a power switch (SPST) and a reverse‑protection diode (1N4007).

Calculate or measure current: PIC + LM35 + DHT22 + OLED + Bluetooth draws roughly 80–120 mA. A 2000 mAh battery gives about 18 hours of continuous operation – enough for a day of field use. Add a low‑voltage warning via the PIC ADC (voltage divider across the battery) to avoid deep discharge.

Sensor Connections

Wire each sensor according to its datasheet. Key points:

  • LM35: Output pin to any analog input (AN0–AN7 on PIC16F877A). VCC to +5 V, Gnd common. No extra components needed.
  • DHT22: Data pin to a digital I/O (e.g., RD0). Add a 10 kΩ pull‑up resistor to VCC. Supply range 3.3–5 V. Must not be connected to I²C or SPI lines.
  • BMP180: SDA – RC4 (on PIC16F877A pin 23), SCL – RC3 (pin 18). Use 4.7 kΩ pull‑up resistors on each line to 3.3 V (or 5 V if BMP180 is 5 V tolerant – confirm with datasheet; original BMP180 is 3.3 V only).
  • OLED (SSD1306 I²C): SDA – same as BMP180 SDA, SCL – same as BMP180 SCL (shared I²C bus). OLED can run at 3.3 V; connect to 3.3 V rail.

Use a common ground plane. Keep analog sensor wires (LM35) away from the boost converter to avoid noise. A small 100 nF capacitor between VCC and GND near each sensor helps decouple.

Display Integration

The SSD1306 OLED communicates over I²C and is controlled via two pins. Initialise the display in your code with a library (e.g., the Adafruit SSD1306 port for PIC). A 16×2 LCD in parallel mode uses many pins – not ideal for portable designs – but an I²C backpack (PCF8574) reduces it to two wires.

Programming the Microcontroller

Use MPLAB X IDE (v5.6 or newer) with XC8 compiler (free version is sufficient). The code structure should be a main loop that:

  1. Reads sensors (LM35 ADC, DHT22, BMP180 via I²C).
  2. Converts raw values to engineering units.
  3. Updates the display every 2 seconds (LCDs refresh slower).
  4. Optionally sends data over UART (Bluetooth) or logs to SD card.
  5. Sleeps in between readings to save power.

Sensor Reading Functions

LM35: Use the PIC ADC module. Configure AN0 as analog input. Read 10‑bit sample, then temperature (°C) = (ADCval * 5000.0) / 1023 / 10. (5 V reference gives 5 V = 1023, LM35 output 10 mV/°C.)

DHT22: This sensor uses a proprietary single‑wire protocol. Implement the timing using TMR0 or a delay routine. Many example libraries for PIC C exist; you can adapt the one from Microchip forums. Typical steps: pull data line low ≥ 1 ms, release, wait for response, then read 40 bits (16 humidity, 16 temperature, 8 checksum).

BMP180: Use the I²C Master mode of the MSSP module. Initialise the module (baud rate 100 kHz). Write and read registers as per the BMP180 datasheet. You need the calibration coefficients (read from registers 0xAA–0xBF) to compute true pressure. Many PIC I²C libraries are available – the one from Microchip Code Configurator (MCC) simplifies setup.

Display: For the SSD1306, send commands via I²C using the same bus. Preload a font table (5×7 pixels) in program memory. Write a function to draw characters at x,y coordinates.

Main Loop Pseudocode (Simplified)

while(1)
{
    // Read sensors (blocking but fast)
    temp_lm35 = readADC(0);
    if (readDHT22(&humidity, &temp_dht) == OK)
        checkChecksum();
    pressure = readBMP180();

    // Convert
    tempC_lm35 = (temp_lm35 * 5000.0) / 1023.0 / 10.0;
    tempC_dht = temp_dht / 10.0;  // DHT gives tenths

    // Update display
    clearDisplay();
    displayString(0,0,"Temp: ");
    displayFloat(tempC_dht,1);
    displayString(0,1,"Hum: ");
    displayFloat(humidity/10.0,1);
    displayString(0,2,"Press: ");
    displayFloat(pressure/100.0,0);
    updateDisplay();

    // Optionally send via UART
    printf("T=%2.1f H=%2.0f P=%4.0f\n", tempC_dht, humidity/10.0, pressure/100.0);

    // Delay (sleep mode possible)
    __delay_ms(2000);
}

For battery savings, consider using the PIC’s SLEEP mode between readings with a watchdog timer or external RTC wake‑up. The OLED can be turned off via a MOSFET or by sending a sleep command (SSD1306 supports display off command).

Calibration and Testing

Before final assembly, test each sensor alone:

  • Place the LM35 in a glass of ice water (0 °C) and boiling water (100 °C, at sea level) to check linearity.
  • Compare DHT22 readings with a known accurate hygrometer. If offset exists, apply a software correction (e.g., ±2% RH).
  • BMP180 altitude readings can be verified against a known reference (weather station). Use the standard pressure‑to‑altitude formula.

Noise reduction tips:

  • Use a 100 nF capacitor across the LM35 output to ground.
  • Average multiple ADC readings (e.g., 8 samples) for each sensor.
  • Keep DHT22 data line away from the boost converter inductor.

Enclosure Design

Portability demands a rugged, weatherproof case. Options:

  • Pelican 1040 Micro Case – watertight, fits a 100×50 mm PCB.
  • 3D‑printed box (PLA or PETG) with a gasket for the lid. Add a 3D‑printed solar shield for the outdoor sensors (white or reflective).
  • ABS junction box with cable glands for sensor wires.

Cut a hole for the OLED; cover it with clear acrylic or a waterproof membrane that doesn’t block the view. Provide a small vent for the DHT22 (drill holes and cover with fine mesh).

Advanced Features

Once the basic station works, you can add these capabilities without major hardware changes:

Wireless Data Logging

Connect an HC‑05 Bluetooth module to the PIC UART (TX/RX). Use a terminal app on your phone to receive real‑time data. For long‑range logging, use an ESP‑01 (ESP8266) running AT firmware – send data to a local MQTT broker or Thingspeak.com. The PIC communicates over UART with the ESP.

SD Card Logging

Add an SPI‑based microSD card module (e.g., with a 3.3 V regulator). Use FAT16/32; the Petit FatFs library (tiny) fits in PIC memory. Log timestamped CSV files. You can later import into Excel for analysis.

Solar Power

A small 5 V solar panel (e.g., 6 V 200 mA) connected through a TP4056 Li‑ion charger module allows daytime charging. Add a Schottky diode to prevent reverse current at night. The battery then powers the station continuously.

Multiple Sensor Nodes

If you build several stations, use the PIC UART to add an RF transmitter (e.g., nRF24L01+). One central receiver logs data from all nodes. Note that nRF24L01+ is 3.3 V; use level shifting.

Conclusion

A PIC‑based portable weather station is a practical project that demonstrates sensor interfacing, I²C/1‑Wire protocols, power management, and field‑ready construction. The microcontroller’s low power consumption and wide operating voltage make it ideal for battery‑powered applications. By following the expanded steps above—choosing compatible parts, wiring carefully, writing robust firmware, and enclosing everything in a weatherproof case—you can build a device that delivers reliable environmental data wherever you go. Experiment with different sensors and wireless options to tailor the station to your needs. For further reading, consult the official datasheets and application notes from Microchip and Bosch Sensortec.