Skip to content

10. Input Devices

This week is an intressting to play with all kind of input sensor. Moreover, the task is to make own sensor and try to get some data and measure.

Due to corona alert in India, my lab is being closed for months.

so, I tried to design my pcb(sensor) by Kicad, so far I haven’t design till DAY 5.

From DAY 1 till DAY 3, I spend my time in learning c, python, assembly language. This have been documented in week09(embedded programming)

In Day 4

I did my 1st-LDR Programming by using “Arduino IDE”

This is the video of LDR(sensor) detection .

Here, I have connected my LDR to an analogpin and connected a LED to a digitalpin.

This is the code. (As per Arduino)

int LDR = A0; //assigning pin for ldr
int LED = 7;  //assigning pin for led
int reader;   //reading the analog value

void setup(){

Serial.begin(9600);
pinMode(LDR,INPUT);
pinMode(LED,OUTPUT);
}

void loop(){

reader=analogRead(LDR);
Serial.println(reader);

if(reader>=250){
    digitalWrite(LED,HIGH);
  }
else {
    digitalWrite(LED,LOW);
}

}

This is the design in Ki-cad Software.

With This video in youtube. I learn to create a Virtual Objects By using python.

This is the Code

from visual export *

shoebox = box(length = 10, width = 7, height = 5, color=color.red, pos = (-5,0,0))
name = label(text = "This is a shoe box", color = color.white, height = 30, box = false, pos = (0,10,0))

With this a video, I created my 1st Virtual object in Python. By using Python VIDLE and Anaconda as a texteditor. For Virtual Enivornment, I am using VPython.

This is a image

Then, In day 4, got a ultrasonic sensor from my kit. then started to measure it and also tried to add virtual effects to it.

This is the Code for the Ultarsonic sensor(in arduino program)

#include <ResponsiveAnalogRead.h>  // for reducing noise, i have included this command 
int trigpin = 13;   //initiallizing pin13(triger)
int echopin = 11;   //initiallizing pin11(echo)

float pingtime;
float speedofsound = 352.39;
float targetdistance;


void setup() {

 Serial.begin(9600);
 pinMode(trigpin,OUTPUT);
 pinMode(echopin,INPUT);

}

void loop() {

  digitalWrite(trigpin,LOW);
  delayMicroseconds(2000);
  digitalWrite(trigpin,HIGH);
  delayMicroseconds(20);
  digitalWrite(trigpin,LOW);

  pingtime = pulseIn(echopin,HIGH);
  pingtime = pingtime/100.;

  targetdistance=speedofsound*pingtime;
  targetdistance=targetdistance/2.;
  targetdistance=targetdistance/10.;

  Serial.println(targetdistance);
  delay(100);

}

This the code for Virtual Enivornment. It is in python

import serial
from visual import *

arduino = serial.Serial("/dev/cu.usbmodem14101",9600)
rod     = cylinder(length = 6, radius = 1, color=color.yellow, pos = (-3,0,0))


while (1==1):
    if(arduino.inWaiting()>0):
        data = arduino.readline()
        distance = float(data)
        print distance

so, while I run this program. there was no output in the GUI. Then, I searched in internet.

I that some people said about, version of python, might cause a problem or try to run the program in terminal. I should do and see the outcome.

So far, I have completed till here and I am facing problem in the virtual enivornment, where my virtual object is not seen.

This is the video of the process.

meanwhile, I talked with my instructor and searched about the GUI of the python3. Also, I was neil’s python simulator. It was Tkinter. Then, I started learn tkinter codes.

then for exploring other Sensor.

I got my old arduino kit from my friend. Actually, we used to play with it in school and I gave to him(during my High school days in boarding school).

In that, I had color sensor, mic, DHT temperture sensor , joystick.

I was choose color sensor, Initially to play.

This is the code, for the Color Sensor( in Arduino )

int S3  = 8;      //Initiallizing S3 to the pin 8 in arduino
int S2  = 7;      //Initiallizing S2 to the pin 7 in arduino
int out = 4;      //Initiializing out to the pin 4 in arduino

unsigned int pulse;

int redcolor;     // Assigning  Values for RED 
int greencolor;   // Assigning  Values for GREEN
int bluecolor;    // Assigning  Values for BLUE

void setup() {

  Serial.begin(9600);  // to ON the Serial Monitor
  pinMode(S2,OUTPUT);  // Initializing pins modes 
  pinMode(S3,OUTPUT);  // Initializing pins modes 
  pinMode(out,INPUT);  // Initializing pins modes 

}

void loop() {
  //1st we should assign Digital wire to the S2 and S3 pins
  // For redcolor light the S2 & S3 are LOW
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);

  pulse = pulseIn(out,LOW);

  redcolor = pulse/400. - 1;
  redcolor = (255 - redcolor);

   //2nd we should assign Digital wire to the S2 and S3 pins
  // For greencolor light the S2 & S3 are HIGH
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);

  pulse = pulseIn(out,LOW);

  greencolor = pulse/400. - 1;
  greencolor = (255 - greencolor);

   //3rd we should assign Digital wire to the S2 and S3 pins
  // For bluecolor light the S2 is LOW & S3 is HIGH
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);

  pulse = pulseIn(out,LOW);

  bluecolor = pulse/400. - 1;
  bluecolor = (255 - bluecolor);

if (redcolor>greencolor &&  greencolor>bluecolor){

     redcolor   = 255;
     greencolor = greencolor/2.;
     bluecolor  = 0;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
  }

if (redcolor>bluecolor &&  bluecolor>greencolor){

     redcolor   = 255;
     greencolor = 0.;
     bluecolor  = bluecolor/2.;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
   }

if (greencolor>redcolor  &&  redcolor>bluecolor){

     redcolor   = redcolor/2.;
     greencolor = 255;
     bluecolor  = 0;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
  }

if (greencolor>bluecolor  &&  bluecolor>redcolor){

     redcolor   = 0.;
     greencolor = 255;
     bluecolor  = bluecolor/2.;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
  }

if (bluecolor>redcolor &&  redcolor>greencolor){

     redcolor   = redcolor/2.;
     greencolor = 0;
     bluecolor  = 255;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
  }

 if (bluecolor>greencolor &&  greencolor>redcolor){

     redcolor   = 0;
     greencolor = greencolor/2.;
     bluecolor  = 255;

     Serial.print(" R : ");
     Serial.print( redcolor );
     Serial.print (" , ");
     Serial.print(" G : ");
     Serial.print( greencolor );
     Serial.print (" , ");
     Serial.print(" B : ");
     Serial.println(  bluecolor );
     Serial.println("  ");
  }


}

Actuallly I learned this code from this tutorial

With that learning I made this.

But due to the poor quality of the Color sensor, I couldn’t get the readings of green color. From the working process video. You will understand.

Here is the Video of the Working.

Then I moved to JOYSTICK. Programming a joystick is very easy.Initailly, I thought it was complex, But while doing it. it is simple.

Coding for Joystick is here

    #include <ResponsiveAnalogRead.h>

    int SW_joy = 5;

    int x_joy  = 0;

    int y_joy  = 1;

    int Xreader;

    int Yreader;

    int Switch;

  void setup() {

     Serial.begin(9600);

     pinMode(SW_joy,INPUT);

     digitalWrite(SW_joy,HIGH);            // I don't I have written this

  }

   void loop() {

     Xreader = analogRead(x_joy);
     Yreader = analogRead(y_joy);
     Switch  = digitalRead(SW_joy);

     Serial.print("switch : ");
     Serial.println(digitalRead(Switch));
     Serial.println("");
     Serial.print(" X - axis: ");
     Serial.println(Xreader);
     Serial.println("");
     Serial.print(" Y - axis: ");
     Serial.println(Yreader);
     Serial.println("");
     delay(3000);
   }

In this code, I got the joystick output, but I did’nt get the switch output.

Moreover, I neeed to debug this issue.

Here is the Video of the Working.

And I also planned to do DHT temperature sensor. Which I should do.


Last update: July 18, 2020