W10 - Input Devices

GROUP ASSIGNMENT

Setup

To measure the power consumption of a device with a multimeter you have turn the knob to the uA~/- or mA~/- positions and use the fused positive connection of the multimeter, plug the probes in series with the VCC of your device and turn it on.

After stabilizing you can check the multimeter to see how much current it is drawing from the power source. In the case of an ESP8266 it was 122uA in sleep mode.

INDIVIDUAL ASSIGNMENT

I want to do something perhaps not so fashion as I would like, but for useful for my final project.

I´m goung to do the S.H.I.E.L.D for my funduino as part of my final project that controls the emergency lightning.

In my final project an street lamp turns blue to annoy the loud people on the street, and red to mark the spot where a gunshot has been fired. This color change is control by an arduino which controls a relay that switches the light color. in order to know if is day or night I’ll use a light sensor, as the color light wont be perceived in daylight, there is no point to switch the light’s color.

The sensor will be made with an phototransistor and a pullup resistor of 1K, measuring Vce. If there is light, there will be a current from C to E and the Vce will be practically 0, if there is no light there will be barely no Ice, only dark current or perhaps an small amount due to the street lamp, (the position of the sensor must be taken into account). So pretty much will Vce be 0. In a real situation the probably I would use an output from the light control to know if light can be changed or not.

I used a SHARP phototransistor 900nm, and a Resistor of 10M to get a wider range of signal entering my ANALOG INPUT. To know more about fototransistors click right here.

The aethernal question of, shall I design my board arquitecture so the system is mostly controlled by hardware, ie. to connect the phototransistor to the relay subsystem in order to enable or disable it directly via hardware or connect both subsystems to different arduino pins and control everything via software. Independently of the FP requirements I find in this case better to control everything with the arduino, sorry Funduino, it complies with the assignment and moreover allows more freedom, as for example, otherwise I would have to pick an specific resistor for every location´s where I would plant the product, as every place has its own lightning conditions, via software… I just have to adjust one line of code of the threshold settings after I´ve characterized the sensor response.

I designed one board for W10 and W11 together (I/O) for convenience. board sch

The datasheet was useful to check if the sensibility range peak was to far away from the visible length spectrum. why, because let’s remember that this is a 900nm photodiode, its peak sensitivity lays in the infrared spectrum, but still has a good sensitivity in the visible light, to know if its dusk or dawn it is enough.

The data sheet was also handy to orient the photoresistor:

The code:

int sensorPin = A0;    // select the input pin for the potentiometer
/*int ledPin = 13;*/      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
/*  pinMode(ledPin, OUTPUT);*/
  // begin the serial monitor @ 9600 baud
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

  Serial.println(sensorValue);
  Serial.print(" ");

  delay(20);
}

I had some trouble making it work.

At another attempt it gave me 5V all the time (1023)!

I debug it, Oscar debugged it, nothing, I check the phototransistor’s behaviour without voltage with the tester.

About phototransistors

It works, so why keeps showing 1023 all the time. This means that I’m readig 5V, that means that there is no current flowing, that means that the base current (the one provided with the light) is greater than the maximum that can be amplified, therefor the phototransistor is saturated by the amount of light, but I find it odd.

I’m going to change the resistor for an smaller one.

Agggg!! the resistor was damaged!! when as I desolder it the metal connector of one side of the resistor came out easily maybe the resistor was damaged. As I explained in W4 the problem was that the soldering iron temperature was to high I put another resistor equal to the one before.

Light Theremin

I made a Theremin using as an input a phototransistor to control the volume and an LDR to control the frequency (note).

I´m using an Attiny85

Fist I designed in eagle and then I milled it.

I solder first the Attiny and the SPI connector to program the chip alone, because if there is any problem I wont waste time. It is easy to intervene the board with estra components or repare it not looking as much as a frankenstein.

I try to burn it using a arduino Uno s an ISP,following this tutorial

board sch

Following this connection scheme:

It doesn’t work!!

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x01
avrdude: stk500_initialize(): (a) protocol error, expect=0x14, resp=0x10
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: stk500_disable(): unknown response=0x12
Error while burning bootloader.

I check the pins and there are 5 volts in the pin RESET, if I disconnect the arduino from that pin the voltage still there, I checked before for shortcircuits and did it again now, there is none, therefor I asume that fe Attiny85 is the one puting them there. It makes no sense, despite the fact that the reset is negative logic, the chip shouldn’t force anything there.

The reset is used when burning the chip to put him in burning mode.

I put a 10uF capacitor between Reset and GND hoping it works as a decoupling capacitor. I try it again… it doesn’t work.

I also put a Capacitor 10uF between the arduino ISP GND & RESET,

IT WORKED!!! (Actually the Theremin capacitor was not really necesary)

To use the device the reset pin has to be disconnected otherwise the device is in “program enable mode”

////
//BLUETOOTH
#include <SoftwareSerial.h>
#define RxD 1// for me 1 pero asi puedo usar PWM
#define TxD 2// for me 0
#define DEBUG_ENABLED  1
   //int led=4

SoftwareSerial blueToothSerial(RxD,TxD);
//setup BT
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here

  delay(2000); // This delay is required.
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  blueToothSerial.print("bluetooth connected!\n");

  delay(2000); // This delay is required.
  blueToothSerial.flush();
}


//// End of BT

int PWM_pin = 0; // Speaker on PB0 (pin 5) of ATtiny85
const int ldrPin = 4; //Note
int LDR=0;

const int photoTPin=3;//Volumen
int PhotoT=0;

void setup() {
  //
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();

  //pinMode(led,OUTPUT);
  //digitalWrite(led,HIGH);
  //
  pinMode(ldrPin, INPUT);
  pinMode(photoTPin, INPUT);
  pinMode(PWM_pin, OUTPUT);
}

void loop() {

  //PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin); //Change in frequency

  int dtpause = 20%LDR; //resultin F
  int L1=10, L2=210, L3=110; //Volume min

  analogWrite(PWM_pin, L2-PhotoT); //Wave
  blueToothSerial.write(L2-PhotoT); //TXwave
  //recvChar = blueToothSerial.read();
  delay(dtpause);//Frequency

  //analogWrite(PWM_pin, L2);
  //delay(dtpause);
  //analogWrite(PWM_pin, L3);
  //delay(dtpause);
}

So I tested, but doesn’t work! It gives me a continuous note and doesn’t change despite the fact that both light sensors work!

After several tests I noticed that the PWM was high after the delay I forgot to make it go back to 0!!

   analogWrite(PWM_pin, L1-PhotoT); //Wave
   delay(1000);//test
  analogWrite(PWM_pin, 0);
  delay(1000);

Now at least the pwm works lets see if the modulation works,

And nothing was working I decided to check what was going on through the serial port, as I have already prepare my board to communicate with Bluetooth I use that as serial monitor.

REMEMBER TO CHANGE YOUR PORT, IS NOT THE ARDUINO ANYMORE, IS THE FTF! (And take the Reset pin out)

Now I can monitor what is going on with the pins.

PHOTORANSISTOR seems to work define light:

no light:

But LDR not really responding always 1

Ok first I check PWM AND VOLUME are working

void loop() {

  PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin); //Change in frequency

  int dtpause = 2; //3, 1 resultin F 400Hz=period of 2, half a period of 1
  //int L1=10, L2=210, L3=110; //Volume min

   analogWrite(PWM_pin, 10); // 110 210 Wave
   delay(dtpause);
   analogWrite(PWM_pin, 0);
   delay(dtpause);

I don’t’ pretend do have an actual note, just to detect variation in volume and frequency, but interestingly, 2 would correspond to a wave of 800Hz (one semiperiod high of 2 milliseconds and one semiperiod low of 2 ms) but sounds more like a 400hz note, when I change the semiperiods (delay) to 1, it sounds one octave higher, is like if it was shifted one octave up, this could be due to the quality of the piezoelectric. The important is that a shift is felt and sounds like the difference is a full octave.

Now I check again the min and max readings of the PHOTORANSISTOR

void loop() {

  PhotoT=analogRead(photoTPin);
  //LDR=analogRead(ldrPin); //Change in frequency

  blueToothSerial.println(PhotoT);

It goes from 86 to 1023

Now I check the readings of the LDR as well in a similar way

I only see 1’s

I check with the tester and the readings are 5v to 3 volts, therefore should be able to see it, I try by multiplying what I read in pin 4 by 10 and it worked

void loop() {

  //PhotoT=analogRead(photoTPin);
  LDR=analogRead(10*ldrPin); //Change in frequency

  blueToothSerial.println(LDR);

Now the readings of LDR are from 90 to 1002

Probably is a issue of bit shifting

Here is a sample of readings

Now I test the VOLUME using the Map function, originally I did it with math, but this is cleaner and easier to modify.

int LDR2;
void loop() {

  PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin*10); // 10*ldrPin Change in frequency

  LDR=LDR/10; //this is not the same, as with the multiplication passes from 1 to lets say 750, and then after dividing passes to 75!!
LDR2=map (LDR,0,100,10,210);


  int dtpause = 2; //resulting F

   analogWrite(PWM_pin, LDR2); //Wave
   delay(dtpause);
   analogWrite(PWM_pin, 0);
   delay(dtpause);

The discrepancy in volume is noticeable.

Checking the frequency:

``` void loop() {

PhotoT=analogRead(photoTPin); LDR=analogRead(ldrPin10); // 10ldrPin Change in frequency

LDR=LDR/10; LDR2=map (LDR,0,100, 0,250); PhotoT2=map (PhotoT,400,1023, 1,6);

//blueToothSerial.println(“LDR”); //blueToothSerial.println(LDR2); blueToothSerial.println(“PhotoT”); blueToothSerial.println(PhotoT); blueToothSerial.println(“dtpause”); blueToothSerial.println(dtpause);

dtpause = 1*PhotoT2; //resultin F //int L1=10, L2=210, L3=100; //Volume min

analogWrite(PWM_pin, 100); //Wave delay(dtpause); analogWrite(PWM_pin, 0); delay(dtpause);

```

Testing that both the frequency and the volume are modified depending of the light, I use a low frequency to make it more noticeable:

I decided to invert the modifiers (LDR2 and PhotoT2) and put in charge of the frequency the LDR, this is because it has less noise and although it was more convenient theoretically to give a wider range of frequencies, I would need to put a low pass filter and anyway I only move from 1 to 6 octaves up.

PhotoT

LDR

This is the final program with serial/bluetooth communication that will be explained in w13

////
//BLUETOOTH
#include <SoftwareSerial.h>
#define RxD 1// for me 1 pero asi puedo usar PWM
#define TxD 2// for me 0
#define DEBUG_ENABLED  1
   //int led=4

SoftwareSerial blueToothSerial(RxD,TxD);
//setup BT
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here

  delay(2000); // This delay is required.
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  blueToothSerial.print("bluetooth connected!\n");

  delay(2000); // This delay is required.
  blueToothSerial.flush();
}


//// End of BT

int PWM_pin = 0; // Speaker on PB0 (pin 5) of ATtiny85
const int ldrPin = 4; //Note
int LDR=0;

const int photoTPin=3;//Volumen
int PhotoT=0;

void setup() {
  //
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();

  //pinMode(led,OUTPUT);
  //digitalWrite(led,HIGH);
  //
  pinMode(ldrPin, INPUT);
  pinMode(photoTPin, INPUT);
  pinMode(PWM_pin, OUTPUT);
}
int LDR2=0;
int PhotoT2=0;
int dtpause =0;

void loop() {

  PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin*10); // 10*ldrPin Change in frequency

  LDR=LDR/10;
LDR2=map (LDR,0,100, 1, 6);
PhotoT2=map (PhotoT,400,1023, 0, 6);

  //blueToothSerial.println("LDR");
 blueToothSerial.println(PWM_pin);
 // blueToothSerial.println("PhotoT");
  //blueToothSerial.println(PhotoT);
  //blueToothSerial.println("dtpause");
  //blueToothSerial.println(dtpause);

  dtpause = 1*LDR2; //resultin F
  //int L1=10, L2=210, L3=100; //Volume min

   analogWrite(PWM_pin, PhotoT2); //Wave
   blueToothSerial.println(1);
   delay(dtpause);
   analogWrite(PWM_pin, 0);
   blueToothSerial.println(0);
   delay(dtpause);

}

I try it completely unplugged from the arduino to prevent any obnoxious comments of oh but you are using the arduino, yes is plugged to the arduino because I’m uploading program variations.

Here is the theremin only VCC and GND.

I was surprised that when I unplugged miso sometimes the frequency became a continuous “1”, I thought it may have to do that the pin stayed in high impedance, when the pin was “on air”, but I corrected the program, inverted once again LDR for the volume and PHOTOTRANSISTOR for the frequency as I originaly intended, but with a beter maping accordingly with the light in fablab (previously was at home) and discovered that LDR and PHOTOT reached negative numbeers which coincided with the problem. Once I solved that with the mentioned remaping, the problem disapeared.

here is the FINAL video

AND PROGRAM

void setup() {
  //
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();

  //pinMode(led,OUTPUT);
  //digitalWrite(led,HIGH);
  //
  pinMode(ldrPin, INPUT);
  pinMode(photoTPin, INPUT);
  pinMode(PWM_pin, OUTPUT);
}
int LDR2=0;
int PhotoT2=0;
int dtpause =0;

void loop() {

  PhotoT=analogRead(photoTPin);
  LDR=analogRead(ldrPin*10); // 10*ldrPin Change in frequency

  LDR=LDR/10;
LDR2=map (LDR,0,100, 1, 6);
PhotoT2=map (PhotoT,0,1023, 0, 100);
//LDR2=map (LDR,0,100, 1, 5);
//PhotoT2=map (PhotoT,400,1023, 0, 4);


  dtpause = 1*PhotoT2/30; //resultin F
  //int L1=10, L2=210, L3=100; //Volume min

   analogWrite(PWM_pin,25* LDR2); //Wave
  // blueToothSerial.println(1);
   delay(dtpause);
   analogWrite(PWM_pin, 0);
  // blueToothSerial.println(0);
   delay(dtpause);