Sensing The World With HIDIOT

By Steve Lord in Electronics | October 26, 2017

Sensing The World With HIDIOT

Sometimes you want things to only happen when the world around you changes. In electronics, we use sensors to detect these changes. In this post we’re going to look at a couple of sensors, how they work and we’ll show you how to use one with your HIDIOT.

This blog post contains all the background info you need with some useful links, but if you just want to get up and running with a light sensor, Steve’s made a video for you to play along with.

Analog and Digital

At an electrical level we have two ways of sensing, analog and digital. Our HIDIOT uses pulses of electricity for communication, and (mostly) steady electrical pulses for power. An Analog sensor will typically generate an electric current with a variable voltage when something happens to it.

The range of possible output of analog sensors is huge. Sometimes we need to convert this to a level our HIDIOT can use. We can use an amplifier to increase the range of the signal received, and a voltage divider to reduce the signal range. If you’ve ever used a pair of headphones, you’ve used an amplifier to control the voltage signal to your headphones.

When we connect an analog sensor to a microcontroller, we have to use a pin that has an Analog to Digital Converter, or ADC to turn that voltage into something readable.

CdS Light Dependent Resistor

A good example of an analog sensor is the classic CdS Light Dependent Resistor (LDR), also known as a photoresistor. These sensors contain toxic metals that react to light, and have different values depending on their use. Because of their toxicity, they’re sometimes a little difficult to get in the EU.

A digital sensor usually sets an output voltage high or low when something is sensed. Often the sensor itself is analog, but the sensor contains a bit of extra circuitry to turn that into a digital signal. For example, a hall effect sensor could set a voltage HIGH when a window is opened, and LOW when closed.

TSL2561

The TSL2561 is an example of a digital sensor. Instead of pulling a voltage off directly, we query the sensor using the i2c bus protocol. Because the electronics to analyse the output are on the sensor itself, it’s able to provide a pre-calibrated and more accurate measurement. We also don’t need to use an analog pin for the sensor. As a bonus, we can chain I2C devices together without using extra pins. If you want to create a multi-sensor, using digital sensors will make things a lot easier for you.

Light And Dark

Digital sensors are fairly easy to use, and we’ll cover i2c separately. Lets try setting up an analog sensor instead. In this case we’ll use a GL55 series CdS photoresistor.

The HIDIOT’s ATTiny85 has 3 free pins available for us to use, called PB0, PB1 and PB2. We can use other pins, but they come with certain restrictions on how and when we can use them. Because the CdS photoresistor is an analog sensor we’ll want to use a pin with an ADC. In this case. PB2 has an ADC, so we’ll use that.

We’ll use a voltage divider circuit to get a reasonable range of light and dark measurements. A voltage divider is easy to build. It’s just two resistors in series. Afrotechmods made a brilliant video that discusses everything you ever wanted to know about how voltage dividers work.

In this case, we’ll use a resistor of about 1 kilo-ohm and a CdS photoresistor as our second resistor. As light hits the photoresistor, it’s resistance increases. This causes our divider’s ratio to change, which changes the voltage between the two resistors. We can then connect PB2 to the point between the two resistors and measure the voltage to give us an indicator of how light or dark it is.

The value we read is dependent on 3 things:

  1. The voltage and current coming into the circuit (5v)
  2. The first resistor’s value (1k)
  3. The amount of light hitting the photoresistor (or more accurately, the second resistor value)

If you wanted a fixed light reading using measurements like Lux (which is a measurement of a surface’s illuminance, or light level) then you’re better using a digital sensor like the TSL2561, but a photoresistor is perfectly fine for checking light and dark once you have a few readings at different levels.

Building our Circuit

Our circuit is based on the connecting a photoresistor example on Pighixxx’s amazing Arduino Basic Connections (ABC) site. ABC is a brilliant resource for ideas on how to extend your HIDIOT with only minor changes.

Connecting a photoresistor

In the diagram above, we can see that at the top, a 5 volt supply is connected to a resistor, with a connection to another 4.7k resistor and on to ground. Between the two resistors is a connection to an analog pin, in this case our PB2 pin. On a breadboard, the connection looks like this:

Breadboard connections

The HIDIOT has two breakout areas, one for each side of the ATTiny chip powering it. The GND, VCC and PB2 pins are used to connect to the breadboard using a set of headers and dupont cables. VCC is connected to column F, row 3 (F3) on the breadboard while PB2 is connected to F5. GND is connected to F7. The photoresistor is connected to J3 and J5. Our 4.7k resistor is connected to I5 and I7.

The HIDIOT supplies power to the circuit, and measures the voltage connected to PB2. As more light falls on the photoresistor, the photoresistor’s resistance drops. The voltage is dependent on the ratio of the first to the second resistor in the series.

Now we have everything set up, the next thing to work on is the software. We’ll use the digikeyboard library to give us a numerical reading of how much light hits our photoresistor.

Reading Light Levels

Our code is really simple, just copy and paste the below into a new project in the Arduino IDE.

Compile the code and prepare to upload. Before you do, just open up a text editor first as every 3 seconds it’s going to type out the value read from the photoresistor. Upload the code and click on the editor window while it’s uploading so the text gets typed in there, instead of all over your code.

If everything’s worked well, you should get a nice list of light readings once per line. Try putting your finger over the top of the photoresistor and it should change. If it stays the same, check your circuit to make sure it matches the breadboard picture above.

Going Further

Now you know how to sense light and dark there’s a lot more that you can do. Here’s some ideas for things to try next.

  1. Modify the code so that the light reading only triggers when the S1 switch is pressed.
  2. Use the S1 switch to trigger a reading, and make it so things only happen when the light is above or below a certain level.
  3. Use PB1 to power a small RGB Light stick, jewel or strip when it’s dark.
  4. Make a kitchen light strip come on when it’s dark and turn off when it’s light.
  5. Use the ATTiny’s sleep capabilities to put the ATTiny to sleep when it’s light and not reading so you can run your light strip off a USB battery charger!

We’re really interested in seeing what people do with their HIDIOTs. If you’ve made something with yours, tell us about it!


Never miss a post

Like what you see?.

Get our latest electronics and security content in your mailbox. We won't send you spam. Unsubscribe any time.