Don’t trust the internet (and how to add an Inductive Proximity Sensor to your 3D printer the proper and easiest way)

Every now and then I still fall for it. Whenever I’m trying to accomplish something I’ve never done before, I start a Google search to find some nice video’s or blog posts that explain how I can accomplish this specific kind of task. That in itself is perfectly fine, however… one should not stop using its own brain!

This week I wanted to add an Inductive Proximity Sensor (LJ12A3-4-Z/BX) to my 3D printer as touching the printer bed while measuring the distance towards the printer bed has an actual effect on the measurement.. duh. Especially when you have a round aluminium printer bed that has supports at three “corners” only.

Side note (1): The main reason I own a 3D printer is that I’m into electronics  – I like to built and program my own IoT hardware – and wanted to be able to print my own custom cases.

So let me refer you to some contents on the internet that explain how to install this proximity sensor:

They all tell you to work with a voltage divider as these sensors are powered by 6V to 36V, which means that their signal wire can be at that level to. Most 3D printer control boards however are Atmega based, which have max input of 5.5V. My MKS Base V1.5 printer board, is indeed based on an Atmega 2560 and suffers that same limitation.

This all seemed very legit, so without thinking, I started to built a voltage divider, following their samples. I started out by combining 6.8kΩ and 4.7kΩ as this does stay within the standard range of “pull-up” values which is between 4.7kΩ and 10kΩ.

Standard voltage divider
Voltage divider v1

Tested my voltage divider with a 9 volts battery, and it all worked out fine. So let’s tear down my printer and add the proximity sensor. Installed the sensor together with the divider and… nothing! I then tested the voltage divider in combination with the sensor (don’t ask me why I didn’t test it with the sensor the first time) and noticed that the output, which should be around 5V, was around 2.5V only. OK, that might be an indication that the supplied current is not enough to maintain the “high” voltage when we are “pissing” away 2.5 mA towards Gnd. That is a bit weird as 2.5 mA is not that much, but let’s give it a try by using some higher value  resistors. I don’t have the 15kΩ and 10kΩ mentioned in the video at hand so let’s take a 68kΩ and 47kΩ!

Voltage divider with very high resistance
Voltage divider with very high resistance

I was amazed as well, but indeed, this seemed to be the solution as the measured voltage now was 4.5V. Still to far off in my opinion, but it is high enough to pass as a logic high so let’s continue. Connect the sensor, configure the printer firmware and see what’s happening! Ehmm… nothing. Even though the sensor’s LED lights up when I approach it with something metal, the printer software still says NO.

Let’s take the digital multi meter at hand again and see what’s going on. We first measure the sensing pin on the printer board. I’m pretty sure I disabled the pull-up in the firmware so we should measure either a floating pin, or something close to GND.

Side note (2): In electronics we do not like floating pins. We either pull them softly, but fully, up towards Vdd or down to GND.

But wait! It reads a high 5V! That’s not good. Verified my firmware and the pull-up is indeed disabled, so did they add some hardware pull-ups?

Please tell me they didn’t. Because if they did add a standard hardware pull-up between 4.7kΩ and 10kΩ , I will indeed never be able to pull it down to LOW through the 68kΩ resistor in my voltage divider. OK, getting a bit tired of it but, never give up and search for the schematics on the printer board then.

You can find some sort of design of the board on GitHub, but that is not detailed enough. Luckily I found someone with the same issue that I had and he had a solution! Hurrah, all I needed to do was to remove some resisters from the printer board and then it will all work. So I prepared my soldering iron to attack my printer board and then……

NOOOOOH, of course not. It was just then when I finally decided to use my own brains. I should be able to solve this myself! Serious? I almost got medieval on the ass of my printer board with a soldering iron.

So let’s use our brain and look at the schematics of these type of sensors.

Sensor_ wiring_NPN_PNP
Sensor wiring NPN PNP

The sensor type I’ve got is NPN NO which means that you measure the BLACK line and:

  • when it is not sensing anything inductive nearby, it should be open and thus floating
  • when it is sensing anything inductive nearby, it will be connected to GND

Hmmm… floating. Remember when I said we don’t like floating pins in electronics? We would like to have either all or nothing. So in this case, the following will most probably be true:

  • when it is not sensing anything inductive nearby, it will be pulled up to Vdd
  • when it is sensing anything inductive nearby, it will be connected to GND

So let’s draw a logical design on how an NPN transistor with a pull-up would look like.

Logical NPN with pull-up
Logical NPN with pull-up

We should be able to measure that with a digital multi meter. Just measure the resistance between BLACK and BROWN. I did and guess what? I indeed measured an almost perfect standard pull-up value of 10kΩ.

But wait a second… that’s why our voltage divider gave such weird values! What we thought was this:

Standard voltage divider
Voltage divider v1

Actually was this:

Actual voltage divider
Actual voltage divider

And that’s why it all didn’t work. Once we started to create a voltage divider with insane values it started to work as the 10kΩ did not have that high of an impact any more.

We can now also answer the question of how we actually should connect our NPN sensor to our printer board. We do need a voltage divider, but one of the resistor values is already given. We can use the following formula to calculate the required value of the second resistor R2:

Voltage divider formula
Voltage divider formula

Or navigate to http://www.ohmslawcalculator.com/voltage-divider-calculator if we are lazy.

The output should be 7142.86Ω.

To be on the safe side we pick a standard value which is less so we end up with 6.8kΩ. Our final solution just looks like this:

Final solution
Final solution

Indeed, all we need to do is add a single 6.8kΩ resistor between BLACK and BLUE and all should work fine.

Once the single resistor was added, I verified all voltages with my digital multi meter and it all worked perfectly fine.

Don’t forget that you do need to change your firmware because we still need to invert our end stop. It is HIGH when not triggered and LOW when triggered.

const bool Z_MIN_ENDSTOP_INVERTING = true;

So after all I got it working in the easiest, cheapest and most logical way one can, simply by using my own brain.

The moral of this story is to never stop thinking yourself and to not blindly trust all stories on the internet. Use your brains!

That being said! Don’t just do as this posts says! First you have to verify which type of sensor you’ve got. If you have a NPN type of sensor, you should be able to measure the 10kΩ pull-up resistor between BROWN and BLACK. If you have a PNP type of sensor, you will most probably(I don’t own one) be able to measure the 10kΩ pull-down resistor between the BLACK and BLUE. In case you’ve got a PNP type of sensor, the very first voltage divider should indeed be used.

Standard voltage divider
Voltage divider v1

One thing is sure. IF you do connect your sensor to 12V (which you should as that is within the specs!) you should not go without any voltage regulation, even though many people suggest you should. It doesn’t matter which type of sensor, you should not connect them as is to your signal wire if the sensor is connected to 12V.

***UPDATE***

Boris had a perfect comment on using a diode instead. And, believe it or not but, using a 1N4148 diode was my first solution. So I did test using a diode and that also works perfectly fine, simply because the direction in which the 12V current would like to flow when the sensor is open will be blocked by the diode. The internal (or hardware) pull-up still pulls the Atmega’s sensor pin to HIGH however.

Current flow when the sensor is open
Current flow when the sensor is open

But as soon as the sensor is closed, the diode would allow the 5V current to flow in the opposite direction towards ground, which pulls the Atmega’s sensor pin to LOW.

Current flow when the sensor is closed.
Current flow when the sensor is closed.

So both the proper voltage divider and a diode can do the job. Which one you prefer… well, the diode might actually be safer to use (they often call it a protection diode with a reason ;-)) if installed correctly, but it also depends on what you have at hand and even what you prefer I guess. The reason I preferred to explain the voltage divider in the first place, is that one cannot wire the resistor in the wrong direction. If you do that on the diode, you will permanently damage your printer board.

***UPDATE 2

While I was watching referrals to this blog post I noticed some Swedish(?) web site. When I looked at that blog post (after translation) it showed an ever better way of connecting the sensor to the printer board by using an opto coupler. I know a lot of people don’t have one of these lying around, but if you do… you can’t be any safer. Check it out here.

***UPDATE 3

Some people were still struggling with the diode implementation, so I created a picture walk-through on how the diode method works. The walk-through can be downloaded over here.

Basic TypeScript Mapping Engine

Just lately I had a need for a mapping engine in TypeScript. I came up with the follwing basic design. I unfortunately couldn’t use it as it works with classes only and our API client generation tool returns types only. It might be of some use for those who do need to map classes however. Have fun with it!

export class MapEngine {
    private readonly mappers: Map<string, any> = new Map<string, any>();

    public get size(): number {
        return this.mappers.size;
    }

    public map<TSource, TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination,
        source: TSource ): TDestination {
            const key = this.createKey(sourceType, destinationType);

            if(!this.mappers.has(key)) {
                throw new Error(`No map defined for: ${key}`);
            }

            const mapper = this.mappers.get(key) as (source: TSource) => TDestination;
            const result = mapper(source);

            return result;
    }

    public set<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination,
        map: (source: TSource) => TDestination): MapEngine {
            const key = this.createKey(sourceType, destinationType);

            this.mappers.set(key, map);

            return this;
    }

    public get<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): (source: TSource) => TDestination {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.get(key) as (source: TSource) => TDestination;

            return result;
    }

    public has<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): boolean {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.has(key);

            return result;
    }

    public delete<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): boolean {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.delete(key);

            return result;
    }

    public clear(): void {
            this.mappers.clear();
    }

    private createKey<TSource, TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): string {
            const result = `${sourceType.name} -> ${destinationType.name}`;

            return result;
    }
}

It can be used like this:

let mapEngine = new MapEngine();
mapEngine.set(Number, String, (source)=> source.toString());
mapEngine.set(Date, String, (source)=> source.toLocaleString());
mapEngine.set(Date, Number, (source)=> source.getTime());


let mappedResult1 = mapEngine.map(Number, String, 15);
console.log(mappedResult1);

let mappedResult2 = mapEngine.map(Date, String, new Date());
console.log(mappedResult2);

let mappedResult3 = mapEngine.map(Date, Number, new Date());
console.log(mappedResult3 );

E1: Driving LED’s without resistors

This is part 1 of a 4 part series on the design and creation of a digital dice that can be used to teach children about electronics and/or programming. The design and training material will be available for free once it’s done.

While I was working on this “project”, I questioned everything and I found myself some unexpected answers. Let’s start with these questions and their answers. It allows you to determine whether or not you are interested in this series at all.

  1. Do we always have to use resistors when we use LED’s in battery powered devices?
    Answer: No.
  2. Is it difficult to program your Microchip (fka Atmel) MCU’s without anything Arduino?
    Answer: No.
  3. Can we reduce the standby power consumption of an ATTiny by so much, that it’s cheaper to not add a power button and keep it on standby forever?
    Answer: Yes.
  4. Can I use basic components to create a device, that allows me to measure current draw in nano amps very easily?
    Answer: Yes.

I will use these questions as the guideline for this series. In every post I will try to cover one question and provide you with a more detailed answer.

This week it’s about driving LED’s without resistors.

Context

The device has to be cheap and not contain components that we do not need. If we can do without resistors, I can cut on both the costs and the complexity. Now can we?

Answer

Yes we definitely can, as long as we select the right LED. The device is powered by a CR2032 3V battery. Thus, if we select a type of LED which has a Forward Voltages (Vf) above 3V, we should be safe.

When we look at the following graph from www.electronics-tutorials.ws we can easily see that a green coloured LED would fit our project perfectly.
I/V Characteristics Curves showing the different colours available

The data sheet for the ultra bright green LED that I had lying around and current measurements I did, supported the graph. I never measured more than around 13 mA of current flowing at 3V.

The ATTiny that I had in mind to control the dice can drive 40mA per output pin and a total of 200mA, so we can run 6 LED’s perfectly fine within the specs and without resistors!

That’s it for this week. Next week I’ll cover programming an Atmel chip without Arduino which is a lot easier than I thought.

Blockchain is bad for the environment

How can a technology be bad for the environment you might ask. Well, here’s why.

Blockchain is as safe as it is, because its security is based on raw computing power. Right now there are thousands of computers around the world that are executing complex calculations to ensure validity of transactions worth a few pennies. Ever thought of that? The people or companies that are making money on blockchain technology are actually trading energy and not in an effective environmental friendly way.

Why do we hype stupid technology?