Close

[Session 5] Coding and Sensor issues

A project log for Connected Trash Can

Device allowing people to see the filling of a trash can thanks to an LED, and sending the data to a website updating live

antoineAntoine 05/19/2022 at 10:520 Comments

Today, we worked on the code to make the LEDs and the sensor work. We installed on the raspberry pi the library Python3 to be able to code in Python, and started making some programs.


To create a program on the root of the raspberry, you need to type "touch name.py" in the console and to edit it, you need to type "sudo nano name.py".

- We made a first one to test the LEDs.  In order to program the GPIOs (the pins of the card) you first need to choose a mode (BCM/BOARD) to choose between the name of the pin or the number of the pin. Once it's done, you need to setup the GPIOs you're going to use. For LEDs, we set them all as OUTPUTs. Then we can code a program and to change the color of the LED, we only have to use "GPIO.output(N°_of_the_pin, True/False)". And that is how we created the following program to test the 3 different colors we needed for the project. 

/!\ The GPIOs give only binary outputs so the value of red, green and blue can only be 0 or Vcc each. To have other values, you should use a DAC (Digital to Analog Converter)

- Then we tried to use the sensor. We first noticed we didn't use the right values of resistors on the ECHO line. We needed the part connected to the mass to be twice the one between the resistors, so we changed to 750 and a 1500 ohms. Then, we copied the following code from the Internet, adapting it to our wiring but we kept getting some strange values. Whatever the distance, the distance displayed in the console was around 8 cm but never a consistent  value, sometimes peaking to 24cm, sometimes lowering to 3cm. We though that the HC-SR04 (ultrasonic sensor) we were using was the problem, so we changed to a US-015, and it worked !

- Now, we know that both LED and the sensor work, so we can start coding the main program. We want it to change the color of the LED according to the distance compared to the size of the trashcan. ANd after a few tries, recodings and tests, we ended up with this first version of the program :

Here is the eletric schem updated with the right sensor and the right resistor values :

For the next time, we'll have to find a way to send the data to server from a Python program and also change the code to be less affected by some strange values that sometimes pop.

Discussions