# 5. E-Textiles and Wearables I This week I worked on a digital sensor making a Button and an analog sensor making a Tilt Potenciometer.

## Digital Sensor

I wanted to create a constellation now that we are working with led lights. I found this tutorial using a Lilypad components and I got inspired by it.

So, I decided to make the Scorpio constellation -( my own b-day present)- using basic components instead of the Lilypad in order to understand how it really works.

How I did it:

1. Draw the circuit.
2. Make a step by step to understand how to build it.
3. Do a components list and search the Data sheet.
4. Calculate the right components for the circuit (Resistances - Battery).
5. Build the circuit and hand craft the final sewing.

## Drawing the circuit Drawing hepled me a lot to understand the logic of the connections and the position of every component. I highly recommend to every beginner to do this before start to sew anything. I tried to draw exactly the way I thought the circuit should be handcrafted.

## Components list - Data sheet - Calculations

By drawing everything you already know what you need. In this case 1. Plane battery: 3V 2. 12 yellow leds: Info here - Size 3mm - Direct Current 20mA - Led Volts 2.1V 3. Conductive Thread: In this case I used Silver thread wich doesn´t have much resistance. Almost nothing 4. Conductive fabric: I used Copper fabric because is highly conductive. Such a cable. 5. Non-conductive fabric: I used a padded polyester fabric. 6. Resistances: 4 with 15 Ohms each one. In the tutorial I red that making groups of 3 for a parallel connection is the right way to do it. As I had 12 yellow leds I needed 4 resistances. This is the calculation of ohms: 1. You will also need to have on hand sissors, double side tape, needle, hoop, cotton fabric to sew the components and non-conductive thread to sew the top constellation. The fabric can be dark blue and translucent.

## Build the circuit

This is how I handcrafted.. ## Analog Sensor

via GIPHY

To understand, I researched what was a potentiometer.
And I found I am making the one called "Pot" that uses a Wipe. The Tilt Potentiometer reads the value of the current passing through a resistive medium (fabric in this case). So, when is connected a semi-conductive fabric to a 5V powersource the Analog Sensor reads the voltage difference values passing from positive to negative and translate that into numbers that can be between 0 to 1023. For this I used: - Breadboard - Wires - Cocodriles - RGB 5mm Led - 180 ohm r - 120 ohm r x 2 - 100 kohm r - Arduino UNO - Resistive fabric (Eeontex) - Metalic button - Conductive Thread

I drew the circuit many times in order to know how the connectios should be. When the circuit was right I started to connect all the components using the breadboard, the Arduino Uno and Eeontonix fabric with this characteristics: Longitud of 11 cm and Resistance of 900 ohms. To Download the Arduino Software and select this options.  ## Coding

Coding to read the potentiometer values: I used the example of AnalogRead

``````int sensorValue = 0; // Theres is no need to add this line but I did for training
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(A0, INPUT); // No need to add this but for training too
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
// print out the value you read:
Serial.println(sensorValue);
delay(1000);        // delay in between reads for stability
}
``````

So, the values I got from the Serial.print were from 0 to 1020 as I was moving the point. When it was close to the positive the number is bigger (1020) and when is close to negative is smaller (0). Now, I analized the values and wanted to turn on the LED colors depending on the AnalogRead. First, the Arduino must identify every RGB color Led as an OUTPUT. Name every pin.. in Void describe what it is.. then in Loop write what is going to happen using the "DigitalWrite" method. I wanted to create a rainbow fade so I had to find the RGB color codes to turn them on or off depending on the AnalogValue red To make this easy for the arduino is better to make sure de Led Pins are connected to PWM. Also for the coding is better to do a Mapping.. so the Arduino will look for certain values and be sure the functions is working.

``````*/
int sensorValue = 0;
int value = 0;

int redPin = 10;
int greenPin = 9;
int bluePin = 6;

int redVal = 255;
int greenVal = 255;
int blueVal = 255;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(A0, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
// print out the value you read:

sensorValue = constrain(sensorValue, 0, 850);

// Go from white to Cyan
if (sensorValue >= 0 && sensorValue <= 250);
{
int CyanColor = map (sensorValue, 0,250,0,255);
redVal = 0;
greenVal = 162;
blueVal = 253;
}

// Go from Cyan to Green
if(sensorValue > 250 && sensorValue <= 450){
int GreenColor = map(sensorValue, 250,450,0,255);
redVal = 0;
greenVal = 255;
blueVal = 0;
// Green  rgb(0,255,0)  250 - 450
}

// Go from Green to Yellow
if(sensorValue > 450 && sensorValue <= 550){
int YellowR = map(sensorValue, 450,550,0,255);
redVal = 255;
greenVal = 235;
blueVal = 0;
// yellow rgb(255,255,0) 450 to 550
}

// Go from Yellow to Orange
if(sensorValue > 550 && sensorValue <= 650){
int OrangeR = map(sensorValue, 550,650,0,255);
redVal = 255;
greenVal = 100;
blueVal = 0;
// Orange rgb(255,255,0) 550 to 650
}

// Go from Orange to Magenta
if(sensorValue > 650 && sensorValue <= 750){
int MagentaR = map(sensorValue, 650,750,0,255);
redVal = 255;
greenVal = 0;
blueVal = 68;
// Magenta rgb(255,0,255) 650 to 750
}

// Go from Magenta to Red
if(sensorValue > 750 && sensorValue <= 850){
int RedR = map(sensorValue, 750,850,0,255);
redVal = 255;
greenVal = 0;
blueVal = 0;
// Red rgb(255,0,0) 750 to 850
}

analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);

delay(10);        // delay in between reads for stability

}
``````

Once compiled and uploaded to the Arduino it looks like this.

After this I wanted to make the color change smooth and nicer. Our local tutor Ianis (I couldnt found this on internet references) explained me how to change the frequency of colors using the mapping too. So I changed the code to this one.

``````
int sensorValue = 0;
int value = 0;

int redPin = 10;
int greenPin = 9;
int bluePin = 6;

int redVal = 255;
int greenVal = 255;
int blueVal = 255;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(A0, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
// print out the value you read:

sensorValue = constrain(sensorValue, 0, 850);

// Go from white to Cyan
if (sensorValue >= 0 && sensorValue <= 300);
{
int CyanColor = map (sensorValue, 0,300,0,255);
redVal = 255 - CyanColor;
greenVal = 255;
blueVal = 255;
// CyanColor rgb ( 0, 255, 255) 0 to 300
}

// Go from Cyan to Green
if(sensorValue > 300 && sensorValue <= 450){
int GreenColor = map(sensorValue, 300,450,0,255);
redVal = 0;
greenVal = 255;
blueVal = 255 - GreenColor;
// GreenColor  rgb(0,255,0)  300 -  450
}

// Go from Green to Yellow
if(sensorValue > 450 && sensorValue <= 600){
int YellowR = map(sensorValue, 450,600,0,255);
redVal = YellowR;
greenVal = 255;
blueVal = 0;
// yellow rgb(255,255,0) 450 to 600
}

// Go from Yellow to Magenta
if(sensorValue > 600 && sensorValue <= 750){
int MagentaR = map(sensorValue, 600,750,0,255);
redVal = 255;
greenVal = 255 - MagentaR;
blueVal = MagentaR;
// Magneta rgb(255,0,255) 600 to 750
}

// Go from Magenta to Red
if(sensorValue > 750 && sensorValue <= 850){
int RedR = map(sensorValue, 750,850,0,255);
redVal = 255;
greenVal = 0;
blueVal = 255 - RedR;
// Red rgb(255,0,0) 750 to 850
}

analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);

Serial.print("sensorValue = ");Serial.print(sensorValue);Serial.print(" | ");
Serial.print("RedVal = ");Serial.print(redVal);Serial.print(" | ");
Serial.print("GreenVal = ");Serial.print(greenVal);Serial.print(" | ");
Serial.print("BlueVal = ");Serial.println(blueVal);

delay(10);        // delay in between reads for stability

}
``````

The interesting here is in every mapping it turns Off or On the led color gradually. For example.. the first color is White. So Red - Green - Blue are ON in a 255 frequency. To convert it in Cyan ( 0,255,255 ) the RED must be turned OFF. So the code says when the Potenciometer sensor reads the values from 0 to 340 the Pin 10 wich is Red will be losing frequency from 255 to 0.

This is exactly the way the leds turn on and off.

## Making the Swatch

"This part is done after I learnt how to make the PCB in the week 11 assignment. So this Swatch is completed lately"

I wanted to do something with it. With the knowledge of the Digital Sensor I had the idea of creating a Wall Decor product but in a professional way. I designed a Bulb that changes the color with the message Create.

!!Pictures of Create!!

I needed:

Hardware - 20 cm Embroidery frame - Embroidery machine - PCB (soft to hard connection) - Female Pins - Conductive Thread - 2 pieces of non conductve fabric - Solder machine and tin

Software - Illustrator

I did draw the circuit again thinking in the space to place it. This time much more simple. Then used Illustrator to make the drawing and the circuit. To make the PCB I measured the distance between the pins of the female connector to be sure they would plug correctly. Then draw it on illustrator and make the negative of the lines and holes. I used the Roland machine with this parameters: In this video you can see the process of the machine. We stuck the Copper layer with a good double side tape so the vibration of the drill doesnt take it off causing problems.

)

This is the result for this basic PCB to connect the components with the Arduino. Then, to sew the circuit and the Bulb design I did save the illustrator file in PNG and used the program Embird with the help of our program assistant Clara and this well explained Tutorial.

We had to create the stitches were the machine is going to sew. Before everything I had to make sure it is able to do the design in the space of the hook wich was 240x200 and save it in the file properties. Put the file in the machine, set it up using the conductive thread in the underneath reel and sew!!  To be honest I didnt found the program easy to use. Sometimes it worked and sometimes didnt. Maybe because it was my first time with the program. But I think must be another way.

Ok, once it was ready I prepared the components to sew them into the embroidery circuit. Just a few notes of my experience so you will pay attention if you want to make it:

• I did the embroidery tree times because I thought it was better but I was wrong. When I sew the components I had tree conductive lines and was dificult to sew it. So I prefered to re-do some of them to make it easier and more clean to understand the circuit. So next time Ill make just one emboidery line for circuits.
• When sewing the components I did connect wrong the resistors .. so be careful when you sew the components.. check many times before sewing.
• I also did wrong the connection of the wires because the PCB is in the opposite side so .. check it many times.
• The last and the most stupid reason why it wasnt working was the code wasnt the right one. BE SURE you save the codes with right names.. better using dates so you know wich is the last one.

Now you can see the process...  