Wednesday 28 November 2018

Raspberry Pi - II

In the second segment on Raspberry Pi, we introduce the camera module. Using the camera, one can click still pictures and also film events. In the first post on Raspberry Pi, we used Raspberry Pi 3 Model B. We will be using the same for all the work in this article as well. Using Python based picamera package, we can communicate with the camera in Raspberry Pi. The code for picamera is here. Documentation to latest picamera release that we will be using is here. It is assumed that a camera that is compatible with this model of Raspberry Pi is already attached. The attached camera is shown below:

























As a first step, we have to enable the camera software on the Raspberry Pi. So, navigate to Raspberry Pi icon --> Preferences --> Raspberry Pi Configuration --> Configure Raspberry Pi system as shown below:




















Then, click on enabled as shown below against Camera:


















Finally, it should look like as shown below:



















Then, click on OK. On the Reboot needed window click Yes:



















Once roboot is complete, login into Raspberry Pi, check version of picamera by running validate.sh that contains below code:

python3 -c "from pkg_resources import require; print(require('picamera')[0].version)"












In the first program in Python, we will see the different properties of the attached camera hardware. The program is shown below:


The output is shown below:

























Note that the default resolution is 720x480, and default value for hflip, and vflip is false. For more details of above properties that can be also set, please refer the documentation. In the next program, we will click a picture. We set a few properties like resolution, hflip, and vflip before clicking the picture and capture it in .jpg format as shown below:












Command to run above program is below:








The captured picture is shown below:




















In the same manner, we can also video record any event and play it in omxplayer in Raspberry Pi. This concludes the post on adding camera module to Raspberry Pi

Friday 23 November 2018

Arduino - VI

We continue the discussion on Arduino. In the sixth post on Arduino, we add the time element to a temperature sensor reading and see the output in serial monitor. We will use the same environment as in the first post of this series

In the last post, by importing a library called TimeLib.h we added the time element to Arduino. For the temperature reading, we will add LM35 sensor. Details about the sensor is here.

The code is shown below:

#include <TimeLib.h>

int tempPin = 0;     // initialize the number of temperature sensor output data pin
int temp_reading;    // declare variable for temperature reading
float temp_celcius;  // declare variable for temperature reading

void setup() {
  Serial.begin(9600);  // initialize serial port
  setTime(1542982538); //The argument to setTime must be Unix time 
}

void loop() {
  temp_reading = analogRead(tempPin);
  temp_celcius = (temp_reading/1024.0)*500; 
  digitalClockDisplay();
  Serial.print(" : ");
  Serial.println(temp_celcius); //temperature in Celcius
  delay(5000);
}

void digitalClockDisplay(){
  // ref https://github.com/PaulStoffregen/Time
  // digital clock display of the time
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year()); 
  Serial.print(" ");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
}

void printDigits(int digits){
  // ref https://github.com/PaulStoffregen/Time
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

The circuit is shown below:




















Upload above program into Arduino. The serial monitor output is shown below:



















This concludes the post on adding time element to a temperature reading in Arduino

Tuesday 6 November 2018

Arduino - V

In this short post on Arduino, we introduce the time element into the serial output. In the previous posts on Arduino, we have used LEDs and interacted with the serial monitor. More often than not, sensors are hooked to such boards and data is read live from these sensors. But, any sensor data makes sense only when the sensor data is captured along with the time. For all the work in this post, we will use the same Arduino Uno that we have used in previous environments.

There are multiple ways we can add the time element into sensor data that is read off Arduino including adding separate hardware, etc. While we may explore various such possibilities in the near future, we now focus on how to add the time element in the simplest possible manner. We do so by importing a new library called Time. Navigate as Tools --> Manage Libraries... as shown below:
















In the Library Manager window that comes up, search for Time as shown below and click on Install button against the latest version 1.5.0. In our case, this library has already been installed. So the Install button is grayed out:
















Once the library is installed, click on Close button. Then, paste below code:


#include <TimeLib.h>

void setup()  {
  Serial.begin(9600);
  setTime(1541502061);        //The argument to setTime must be Unix time 
  Serial.print("The hour in 24 hour format: ");
  Serial.println(hour());            // the hour in 24 hour format
  Serial.print("The hour in 12 hour format: ");
  Serial.println(hourFormat12());    // the hour in 12 hour format
  Serial.print("The minute in 60 minute format: ");
  Serial.println(minute());          // the minute in 60 minute format
  Serial.print("The second in 60 second format: ");
  Serial.println(second());          // the second in 60 second format
  Serial.print("The day of month: ");
  Serial.println(day());             // the day of month
  Serial.print("Day of the week starting with Sunday as 1: ");
  Serial.println(weekday());         // day of the week starting with Sunday as 1
  Serial.print("The month of year with January as 1: ");
  Serial.println(month());           // the month of year with January as 1
  Serial.print("The full four digit year: ");
  Serial.println(year());            // the full four digit year
  Serial.print("Returns 1 if time now is AM else 0: ");
  Serial.println(isAM());            // returns true if time now is AM
  Serial.print("Returns 1 if time now is PM else 0: ");
  Serial.println(isPM());            // returns true if time now is PM
  Serial.print("Returns the current time as seconds since January 1 1970: ");
  Serial.println(now());             // returns the current time as seconds since January 1 1970
}

void loop() {
}

To see the date elements in the serial output, we need to set Unix time as parameter in setTime. It is the number of seconds since 00:00:00 1st January 1970 as is indicated in the last line of above code. Upload the above code to Arduino Uno. The output in serial monitor is shown below:



















We will revisit the time part in later posts. This concludes the post