Showing posts with label languages. Show all posts
Showing posts with label languages. Show all posts

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

Wednesday, 24 October 2018

Raspberry Pi - I

In the next post on IOT, we look at Raspberry Pi, a  low cost, credit-card sized computer. We will be using Raspberry Pi 3 for all the work in this post. The hardware specification and other details can be found here. There is a detailed process for setup and installation of OS and how to make it ready for use. We are bypassing all this as these are available on the net. In our case, we are using Raspbian. The aim of this post is to replicate the traffic signal that we have shown in our first post on IOT.

The code in Python is shown below:















The circuit is shown below:







































We can run the above code as shown below on terminal:



We can see that the red and green LEDs light up alternately:






































Another code to achieve the same effect is shown below:















We can run the above code as shown below:



This concludes the post on Raspberry Pi

Friday, 12 October 2018

Arduino - IV

In the fourth post on Arduino, we see how Python can communicate with Arduino via Serial Port. For the all the work in this post, we will be using the same Arduino Uno and Arduino IDE version 1.8.7. Python version 3.7.0 is used. We will need pyserial library version 3.4 also. Details are shown below:

F:\>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

pyserial details are here. At the time of this line being written 3.4 version is the latest

The Arduino program is shown below:

int data;
int red = 10;

void setup() {
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(red, OUTPUT); //make the LED pin (10) as output
  digitalWrite (red, LOW);
  Serial.println("Message from Arduino setup()");
  Serial.println("Enter 1 for firing up LED");
  Serial.println("Enter 0 for turning off LED");
  Serial.println("Any other number to exit Python program");
}

void loop() {
  
  while (Serial.available()){
    data = Serial.read();
  }
  if (data == '1'){
    digitalWrite (red, HIGH);
  }
  if (data == '0'){
    digitalWrite (red, LOW);
  }
  
}

The above program is uploaded to Arduino board:

























The circuit is shown below:




















The python program used to send input is shown below:

import serial #Serial imported for Serial communication
import time #Required to use delay functions

ArduinoSerialPort = serial.Serial('COM4',9600)

time.sleep(2)

print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())
print(ArduinoSerialPort.readline())

while True:
  var = input() #get input from user
  print("You entered ", var) #print the input for confirmation
  if ((var != '0') and (var != '1')):
    print("Exiting program")
    break
  elif (var == '1'): #if the value is 1
    ArduinoSerialPort.write(b'1') #send 1 to Arduino
    print("LED is turned ON")
    time.sleep(1)

  elif (var == '0'): #if the value is 0
    ArduinoSerialPort.write(b'0') #send 0 to Arduino
    print("LED is turned OFF")

    time.sleep(1)

If the user enters 1 in Python Shell, LED is turned on. Then, if 0 is entered, LED is turned off. If the user enter any other number, then, the program exits

Open above program in IDLE as shown below:

 























To run above program, click on Run Module under Run menu as shown below:

























This will open the Python Shell as shown below and wait for user input:

























Enter 1. This will light up the LED as shown below:












































Entering 0 will turn of the LED as shown below:












































Entering 5 will exit the program as shown below:

























Thus, we have seen how Python communicates with Arduino using Serial Port using pyserial. This concludes the post on Python interaction with Arduino

Thursday, 11 October 2018

Arduino - III

In the last post, we saw the serial monitor output from Arduino. In this post we will take a look at how we can communicate with Arduino by using serial input. We will continue to use the same Arduino Uno and Arduino IDE used in the last post.

The first program that we will see is shown below:

// read from serial input and post it to serial output using println

void setup() {
  Serial.begin(9600);  // initialize serial port
}

void loop() {
  
  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     //for reading from the serial port
    int inByte = Serial.read();    //Reads incoming serial data as bytes
    Serial.println(inByte);        //Prints data to serial monitor in human-readable ASCII text
  }
  
}

Once this program is uploaded onto Arduino board, we can see the outputs for the inputs shown below:

1) Clicking just Send button with no input returns

10

2) Input 0 returns

48
10

3) Input 1 returns

49
10

4) Input Arduino Uno returns

65
114
100
117
105
110
111
32
85
110
111
10

The outputs are shown below:



















10 is returned after every input as it is newline character. To turn it off in output, we can set the No line ending in the first dropdown in right bottom of Serial monitor from the default value of Newline. Also, the output is the ASCII representation on the input characters. Note the 32 in the last output that stands for space character in between Arduino and Uno.

In the next program we look at a similar program but we use write instead of println();

// read from serial input and post it to serial output

void setup() {
  Serial.begin(9600);  // initialize serial port
}

void loop() {
  
  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     //for reading from the serial port
    int inByte = Serial.read();    //Reads incoming serial data as bytes
    Serial.write(inByte);          //Writes binary data to serial monitor
  }
  
}

Passing the same inputs as in above case, we get below output:

1) Clicking just Send button with no input returns nothing

2) Input 0 returns

0

3) Input 1 returns

1

4) Input Arduino Uno returns

Arduino Uno

The outputs are shown below:



















In the last example, we will control a LED by entering an input in Serial monitor:

// read from serial input and control a LED

int red = 10;  // initialize the number of red pin
int inByte;

void setup() {
  Serial.begin(9600);  // initialize serial port
  // initialize digital pin red as an output.
  pinMode(red, OUTPUT);
}

void loop() {

  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     // for reading from the serial port
    inByte = Serial.read();        //Reads incoming serial data as bytes
  }
  if (inByte == '1') {
    digitalWrite(red, HIGH);     //Input of 1 will power on LED
  }
  if (inByte == '0')
  {
    digitalWrite(red, LOW);     //Input of 0 will power off LED
  }
}

The program in IDE is shown below:














The circuit is shown below:




















Once the program is uploaded, sending an input of 1 in Serial monitor will fire up the LED as shown below:




















Sending an input of 0 will power off the LED.

With this example, we conclude this post on Arduino.

Monday, 8 October 2018

Arduino - II

After the first post on Arduino, we will look at commonly used data types and the serial monitor output of these data types on Arduino in this segment. For all the work in this post, we will use the same Arduino Uno and Arduino IDE used in the last post.

In this post, we will take a look at how data generated by Arduino board can be seen on the console. This is important because apart from viewing the data generated by any sensor, a developer can use this feature for debugging the code. We use a very simple program shown below:

// data types

int int_a = 10, int_b = 25;
boolean boolean_c = true, boolean_d = 0;
char char_e = 'a', char_f = 97;
unsigned char unsigned_char_g = 255;
unsigned int unsigned_int_h = 65535;
word word_i = 65535;
long long_j = 2147483647L;
unsigned long unsigned_long_k = 4294967295;
float float_l = 1.1234;
double double_m = 1.1234;
char string_n[7] = "arduino";     // string as array of char
int array_int_o[] = {2, 4, 8, 3, 6}; // array of int
enum enum_p {uno,mega};
enum_p type = uno;
String string_q = "Hello Arduino"; //String object

void setup() {
  Serial.begin(9600); // opens serial port and sets data rate to 9600 bits per second
  Serial.println(int_a);
  Serial.println(int_b);
  Serial.println(int_b,BIN);
  Serial.println(int_b,OCT);
  Serial.println(int_b,DEC);
  Serial.println(int_b,HEX);
  Serial.println(boolean_c);
  Serial.println(boolean_d);
  Serial.println(char_e);
  Serial.println(char_f);
  Serial.println(unsigned_char_g);
  Serial.println(unsigned_int_h);
  Serial.println(word_i);
  Serial.println(long_j);
  Serial.println(unsigned_long_k);
  Serial.println(float_l);
  Serial.println(float_l,1);
  Serial.println(float_l,2);
  Serial.println(float_l,3);
  Serial.println(float_l,4);
  Serial.println(double_m);
  Serial.println(double_m,1);
  Serial.println(double_m,2);
  Serial.println(double_m,3);
  Serial.println(double_m,4);
  Serial.println(string_n);
  Serial.println(array_int_o[1]);
  Serial.println(type);
  Serial.println(string_q);
}

void loop() {
  // put your main code here, to run repeatedly:

}

For integers, we can specify the base as an optional parameter. For float and double, we can specify number of digits to be shown after the decimal as an optional parameter. Once the above code is compiled and uploaded, we get the confirmation that the code has been uploaded to Arduino board:













We can access the serial monitor either by navigating from Tools menu or by clicking the Serial Monitor icon on right top as shown below:









Once the Serial Monitor comes up, we see below output:


















Let us now juxtapose the results against the println commands to validate the outputs:

























Note that booleans are printed as integers and that floats and decimals are shown with two decimals by default. String is not really a data type but an object in Arduino. In the program we have set the baud rate to 9600 and Serial Monitor showed the output at this baud rate only. Let us now set the baud rate to 4800 at the right bottom in the Serial Monitor. We get some strange characters in Serial Monitor:













Setting baud rate to 19200 shows below output in Serial Monitor:













Setting baud rate back to 9600 shows the correct output that we got earlier. With this, we conclude the second post on Arduino

Friday, 5 October 2018

Arduino - I

In the first post on Internet of Things, we explore Arduino. Arduino is an inexpensive, platform independent, open-source electronics platform based on easy-to-use extensible hardware and extensible software. More details on Arduino are here. The language reference for coding in Arduino can be seen here. Arduino IDE is used for coding and the details are here. One can either use the online version or the local version. Link to the online version is here. The binaries for local installation are here. One can download the software corresponding to their OS: Windows, Mac OS X or Linux.

For the all the work in this post, we will be using the very elementary Arduino Uno. Details about Arduino Uno are here. To learn more about Arduino anatomy, one can see here. We will be using USB to power this board. For the IDE, we will use a local install on Windows. We have used this installer. The installation is very straight forward. For any help with installation, one can refer this link. The version is of IDE is shown below:

















At this point, we will assume that we are all set up with the board and the IDE. We enter code as shown in IDE below:




















To support running of the above code, we need a green LED, a red LED, a USB cable, connecting wires, a bread board, and an Arduino Uno board. The corresponding circuit is shown below.




















The circuit itself is elementary. Once we connect the USB cable to the laptop, we can set the board to Arduino Uno as shown below:

























We then set the port as shown below:















Once these two settings are done, we can save the code to a folder and file by navigating File -> Save As... and entering Signal in the window as shown below:






















This file is now saved as Signal.ino under Signal.ino folder. We can now compile this program as shown below:

















Once we compile, we can see the output Done compiling as shown below:










We can then upload the program to the Arduino board by navigating Sketch -> Upload or clicking the -> button just below Edit Menu as shown below:
















Once the code is uploaded, we can see the output as Done uploading as shown below:










We can now see the red and green LEDs light up alternately and not together simulating a traffic signal:






































This concludes our first post on Arduino