Through beam sensor with Digispark (ATtiny85 USB Development Board)

> 3 min read

Through beam sensors can be used for a variety of applications, which include counting objects, number of steps move, etc. In this post, we will be looking at how do one interface this sensor with Digispark (ATtiny85 USB Development Board).


Brief Overview

Digispark (ATtiny85 USB Board)

Digispark is actually a development board with an ATtiny85 and USB connector, which makes programming the ATtiny85 easier. (Save some effort compared to using a programmer to program the ATtiny85.) For the Digispark, it comes loaded with a Digispark bootloader which allows user to program the ATtiny85 directly by plugging the board into the USB port. Below shows the pinout of the Digispark. (For reference)



Beam photoelectric infrared Sensor (LM393)


The Beam photoelectric infrared Sensor is a beam through sensor, which means that whenever an object passes through the sensor, the sensor output state will change accordingly. For this sensor, when the sensor is obstructed, it outputs a digital HIGH. Otherwise, it will output a digital LOW. The simplified workings of this sensor would look something like this, where OUT is the output pin.



Parts Required

Below are the parts required for this tutorial:

  • Digispark
  • Beam photoelectric infrared Sensor
  • Jumper wires
  • Mini breadboard (Optional)

Setting Up

Digispark Driver

Before commencing on the main objective, we first have to setup the Digispark. As the computer would not recognise the Digispark when you plug it in, we have to install the USB driver for it. To do so, download the following package here (Select the DigisparkArduino-Win32-1.0.4-May19). Extract the contents.


Navigate to

DigisparkArduino-Win32 > DigisparkWindowsDriver

and run InstallDriver.exe to start installing the USB driver.

Arduino IDE

We’ll be using the Arduino IDE to program the Digispark. The Digispark uses Arduino IDE 1.6.5 and above, so make sure to upgrade your Arduino IDE accordingly if you need to. For the Arduino IDE, we have to download the board configuration for the Digispark. To do so, go to File > Preferences.


In the preference, insert the following link in the Additional Boards Manager URLs and click OK.


Next, go to Tools > Boards > Boards Manager. Search for Digi and install the Digistump AVR Boards entry.


After installation, goto Tools > Boards again, but this time we would select the Digispark (Default – 16.5mhz) option. This is to tell the Arduino IDE that the board we will be using is the Digispark board.


Hello World

We’ll be uploading the traditional “Hello World” sample into the Digispark to ensure that the board is able to communicate with the computer. To do so, we’ll be uploading a simple blink sketch into the Digispark. (File>Examples>Digispark_Example>Start)

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(0, OUTPUT); //LED on Model B
  pinMode(1, OUTPUT); //LED on Model A   

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(1, HIGH);
  delay(1000);               // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(1, LOW); 
  delay(1000);               // wait for a second

Before uploading the sketch, unplug you Digispark first. Next, press the upload sketch button & wait for it to be compiled. When the console/log at the bottom of the Arduino IDE displays Plug in device now…, plug the Digispark back in.


If the status shows Micronucleus done. Thank you! , it means that the program has successfully being uploaded.


With that, let’s move on to interfacing the sensor with the Digispark.

NOTE: Apparently, the Digispark can only be detected using only USB2.0, so if you plug it in a USB 3.0 port, the computer may not be able to detect the Digispark.



We’ll now be interfacing the sensor with the Digispark. Connect the Digispark and the sensor as shown below. We’ll be using pin P2 for reading the sensor’s output.


Below are a few images of how the connection should look like:



Basically how the Digispark will function is that whenever the sensor is blocked, the LED on the Digispark will light up. If not, the LED will remain off.

 *  Cyan Infinite
 *  Digispark with through beam sensor
 *  Visit /tutorials/through-beam-sensor-with-digispark-attiny85-usb-development-board
 *  for more information.

int state=0;  //Sensor state

void setup() {                
  // initialize the digital pin as an output.
  pinMode(1, OUTPUT); //LED on the board   

void loop() {
  state = digitalRead(2);   //Read the sensor output value
   * Write the state of the sensor output.
   * If sensor output is HIGH -> LED ON
   * If sensor output is LOW -> LED OFF
  digitalWrite(1, state);   
  delay(10);            // Wait for a 10ms



A short demo of the code.

You may also like...