KalyanChakravarthy.net

Thoughts, stories and ideas.

Automated Plant Watering System - Part 2 - PCB Design and Prototyping

After running the prototype 1 for a week, I wanted to scale up this effort for more of my plants. However efforts to prototype on a PCB blank were a complete mess, so I decided to jump the gun.

DSC05718

IMG_0749

Design Goals:

It was clear that running one MCU per plant was not effective, at the same time I wanted configurability and thus compromises had to be made.

  1. Run on AA batteries or 3.7v LiPo cells.
  2. Control at least 2 Motors
  3. Configure using 2 push buttons with ability to bypass it to use other peripherals such as a soil sensor.
  4. Accommodate both Attiny85 bare IC as well as Digispark.

Bill of materials:

  1. Digispark Attiny85 or bare IC.
  2. 2x NPN Transistors (I used BC547 at hand)
  3. 2x Diodes (I used LEDs due to lack of proper diodes)
  4. 2x 3v Submersible DC Pumps
  5. 1x LED for feedback display
  6. 1x battery pack (min 3v)

Design

Design was fairly similar to earlier prototype with just few more ports.

The design phase involved both schematic design as well as PCB footprint design using KiCad. I made use of hierarchical sheets to "re-use" the schematic design for a single motor.

SchematicSummary

I used the "Save/Restore Layout" plugin to do the same in PCB mode.

pcb-summary

Program

It turned out that the use of a push button without a pull down resistor makes it unusable, due to which am now relying on manual programming.

The duration is programmed to be 2 seconds of watering for every 2 hours, which appears to be adequate for my basil and coriander plants

The code itself was similar to earlier version.

PCB Fabrication

The final step was to export Gerber files, containing Image/SVG representation of all individual layers such as copper front and back, silkscreen for info, soldermask, vias, etc.

A key element of design that I missed during first iteration was the edge cuts which is the SVG layer outlining how the PCB has to be cut.

All the exported files were zipped and sent to pcbway. The fabbed PCBs arrived less than a week later. As shipping was the most expensive part of the process, I might perhaps combine multiple designs into a PCB order so they arrive together.

KiCad Note:

Install plugins by copying files from

  • Source: https://github.com/MitjaNemec/Kicad_action_plugins.git
  • Destination (macOS): /Applications/KiCad/kicad.app/Contents/SharedSupport/scripting/plugins/

Automated Plant Watering System - Part 1

As I finally found sometime at hand, I decided to attend to one of my long standing projects to automate watering of my plants, which have been through stressful troughs of famine and crests of over-watering.

DSC05728

DSC05725

Goals

One of the goals were that the system should last long enough while running off a battery. Secondary goals was the use of soil moisture sensors to optimize further.

Bill of materials:

  1. Digispark Attiny85
  2. Common NPN Transistor
  3. 3.7V LiPo
  4. 3V DC Pump
  5. Diode (or LED)

Design:

Its a straight forward circuit with the MCU's output controlling the P of NPN Transistor. The Transistor serves to amplify the current as the DC motors are rated at 200mA while Attiny85 is only able to deliver few milliAmps of current.

I modified Digispark Attiny85 by disconnecting the onboard voltage regulator as it consumes ~1mA of current even when the MCU is in deep sleep. As Attiny85 is rated from 2.7v-5.5v the 3.7v LiPo's 4.1v output would be well within the required voltage range.

A DC Motor is an inductor - so when the motor is turned off, the stored energy in the motor flows back as reverse current. It can be powerful enough to damage the MCU, so a diode connected in reverse is needed. Due to lack of components I improvised by the use of a common LED connected in reverse.

Circuit Diagram

The Program

The MCU is programmed to wake up every second via a watchdog timer interrupt. On wake, it checks if it needs to turn on or turn off the output, performs it and goes back to sleep. This means the MCU is asleep even when the motor is running. The ON/OFF state is maintained with the count of wake up cycles rather than milliseconds - this works as each sleep cycle is 1 second.

Digispark's Attiny85's fuses are set at 16Mhz but the input is not exactly stable due the absense of voltage regulatr, this can lead to drift in timekeeping of few minutes per day.

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>

#define adc_disable() (ADCSRA &= ~(1<<ADEN)) // disable ADC (before power-off)
#define adc_enable()  (ADCSRA |=  (1<<ADEN)) // re-enable ADC

#define MPIN 1

unsigned int lpCntr=0;
unsigned int isOn=0;
const unsigned int onInterval = 2; // 2 sleep cycles
const unsigned int offInterval = 60 * 60; //(60 * 60 * 6) - onInterval; //round it off to 6 hours
const unsigned int repInterval = onInterval + offInterval;

void setup() {
  for(int i=0; i<6; i++) {
    pinMode(i, INPUT);
    digitalWrite(i, LOW);
  }

  adc_disable();
  wdt_reset();          //watchdog reset
  wdt_enable(WDTO_1S);  //1s watchdog timer
  WDTCR |= _BV(WDIE);   //Interrupts watchdog enable
  sei();                //enable interrupts
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}

void loop() {

  // Loop counter reset
  if( lpCntr >= repInterval ) {
    lpCntr = lpCntr % repInterval;
  }

  // Loop counter began, so write high
  if( lpCntr == 0 ) {
    pinMode(MPIN, OUTPUT);
    digitalWrite(MPIN, HIGH);
    isOn=1;
  } 
  else
  if( isOn == 1 && lpCntr >= onInterval ) {
    // Reset the output to LOW if we are done
    pinMode(MPIN, OUTPUT);
    digitalWrite(MPIN, LOW);
    isOn=0;
  }
  
  lpCntr++;

  sleep_enable();
  sleep_cpu();

}

ISR (WDT_vect) {
  WDTCR |= _BV(WDIE);
}

Split mp3's using silence detection

Am listening to audio for practicing dutch comprehension, which essentially consists of a collection of short sentences on repeat. However this audio is available as a single track and for the purpose of shadowing, I wanted individual mp3's.

Enter mp3splt

Install it using brew

brew install mp3splt

Auto detect silence and split

mp3splt -s -d output_folder audio.mp3

This detects the average silence level and uses it to split.

Params:

  • -s - Split with auto detection
  • -d - Output directory

Split at specified silence volume

mp3splt -s -p th=-40 audio.mp3

Params:

  • -s - Split with parameters specified using -p
  • -p - Input parameters for -s split action
  • th - Silence threshold in dB

Split and remove silence

mp3splt -s -p rm audio.mp3

Params:

  • rm - Used in conjunction with -s and -p. Indicates silence is to be removed from the output.

FFMPEG: Combine videos and re-encode

Over the past few weeks, I've taken to recording my Squash games using my Gopro to understand areas of improvement. The video is recorded at 1080p and 60fps, however Gopro splits the video into several large sized mp4 files.

I wanted to combine them together into a single file and re-encoded to reduce the file size significantly and ffmpeg came to rescue.

Combine Files

First order is to combine the files. ffmpeg accepts input text file with each line consisting of the object type ( file  in this case) and the file name. Alternatively it also accepts http and a url as a line item as well.

$ ls *.MP4 | perl -ne 'print "file $_"' > input.txt
$ cat input.txt 
file GOPR4466.MP4
file GP014466.MP4

Encode

To reduce file size one can do several things. The easiest is to set the crf flag which indicates "Constant Rate Factor" or the quality of encoding with values ranging from 0 to 51.  0 indicates lossless encoding and 51 the opposite. The default value is 23

$ ffmpeg -f concat -i input.txt -c:v libx264 -crf 28 output.txt

One-liner

$ ls *.MP4 | perl -ne 'print "file $_"' | ffmpeg -safe 0 -protocol_whitelist "file,pipe" -crf 28 -f concat -i - output.mp4

Books in 2018

Similar to 2016 and 2017, I spent a lot of time reading books. Original goal was to read at least 25, but I managed to read only 24. However, I attribute it to spending a lot time reading genres that were outside my comfort zone, such as philosophy and biographies.

Here is a small collage I built with a python script, of all the titles I managed to read.

Fiction

1. A Thousand Splendid Suns

Hosseini, Khaled

This was by far my favourite in the entire year. The story was beautiful, heart wrenching & soul crushing tragedy. The collateral damage from the wars fought in the name of proving oneself or beliefs better than others, seemed not very far from reality at all.

The book was as evocative as this other book "The Ministry of Utmost Happiness" by Arundhati Roy. They certainly seemed to exist in the same realistic universe.

2. The Moon Is a Harsh Mistress

Heinlein, Robert A.

I ended the year with a quick blaze through the book. And it read like an action movie which you know is terrible but you watch anyway because its fun.

3. Harry Potter and the Cursed Child: Parts One and Two (Harry Potter, #8)

Tiffany, John

Just 6 years ago, I read the Harry Potter for the first time. It was incredibly enjoyable, nostalgia inducing journey to revisit the universe again, despite the book not entirely living up to the hype of the original series.

4. Space Team (Space Team, #1)

Hutchison, Barry J.

5. Use of Weapons

Banks, Iain M.

6. Stories of Your Life and Others

Chiang, Ted

The change of title of the book to "The Arrival", after a short story became a movie, deceived me into purchasing this. I was throughly confused while I went through the first short about the tower of babylon as I wondered how it related to space and aliens.

7. Killing Floor (Jack Reacher, #1)

Child, Lee

8. Artemis

Weir, Andy


Comics

1. Adulthood Is a Myth (Sarah's Scribbles, #1)

Andersen, Sarah

2. Hyperbole and a Half: Unfortunate Situations, Flawed Coping Mechanisms, Mayhem, and Other Things That Happened

Brosh, Allie


Biography

1. Mr. Nice

Marks, Howard

2. Alibaba: The House That Jack Ma Built

Clark, Duncan

3. Believe Me: A Memoir of Love, Death, and Jazz Chickens

Izzard, Eddie

4. The Spy Who Came in from the Cold

le Carré, John

5. Kitchen Confidential: Adventures in the Culinary Underbelly

Bourdain, Anthony


Philosophy

1. Zen and the Art of Motorcycle Maintenance: An Inquiry Into Values

Pirsig, Robert M.

2. How to Break Up with Your Phone: The 30-Day Plan to Take Back Your Life

Price, Catherine

3. How to Walk (Mindfulness Essentials, #4)

Nhất Hạnh, Thích

4. The Hidden Pleasures of Life: A New Way of Remembering the Past and Imagining the Future

Zeldin, Theodore


Non-fiction

1. A Burglar's Guide to the City

Manaugh, Geoff

2. You Are Not So Smart:

Why You Have Too Many Friends on Facebook, Why Your Memory Is Mostly Fiction, and 46 Other Ways You're Deluding Yourself
McRaney, David

Gladwell, Malcolm

4. Good to Great: Why Some Companies Make the Leap... and Others Don't

Collins, James C.

5. How to Talk to Anyone: 92 Little Tricks for Big Success in Relationships

Lowndes, Leil