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