Skip to main content
All CollectionsTroubleshooting guides and FAQsUseful AT commands
Why you shouldn’t tell your IoT modems to attach/detach
Why you shouldn’t tell your IoT modems to attach/detach

Using AT+COPS=0 and AT+COPS=2 to attach / detach modems to / from the network is often a bad idea.

P
Written by Peter Skov Bornerup
Updated over a month ago

Modems come in many sizes and shapes. Common to all is that they are generally quite powerful devices – often with processing power far greater than the MCU managing the connection – and running some sort of embedded Linux distribution has been a typical way to implement the necessary stacks.

However, they can have some quirks that make them difficult to operate efficiently. A lot of these quirks need to be learned through first-hand experience, i.e. through trial-and-error.

One quirk is that, most of the time, you should not manually tell the modem to attach or detach using AT+COPS=0 and AT+COPS=2. Reenabling already enabled features can cause issues.


Let's explore why.

If you’re not familiar with AT+COPS=0 or AT+COPS=2, you can read a full overview of AT+COPS commands here: AT+COPS: Modes, parameters, and examples.


Do not tell the modem to detach.

AT+COPS=2 is the standard AT command to deregister a modem from a registered network.

Counterintuitively, using this command to detach from networks is in 99.99% of the cases a really bad idea. Unless you have oddly specific needs, you really - really - should not manually trigger the modem to detach.


Telling the modem to detach also implies that you do not want to use that network next time you need a connection – or at least that it should pick a new network next time it establishes a connection. Next time the modem is powered on, it will start scanning for networks as described in the standards.

3GPP TS 23.122 and automatic network selection.

This is a slow and power-consuming procedure and not something you will want to occur, in most cases.

Instead of using AT+COPS=2, you should just gently power the modem down. The modem "knows" what to do and will perform a safe detach for you.

While doing that, the modem will involve the SIM and write a bit of data to the EF_LOCI file (stored on the SIM). This file works, more or less, as a local persistent cache and when the modem is powered on again, it’ll use this cache to significantly speed up the network attachment.

The result of explicitly detaching: As a side effect, the current (known) network is cleared from all caches. On the next boot, this triggers a complete network scan.

By powering the modem off, we avoid unwanted side-effects. Instead the current network is written to the EF_LOCI and is stored in non-volatile memory.

The effects of explicit detach are demonstrated in the illustrations above.

The lengthy network scan procedure is more prominent on non-steered SIMs and might not be an issue if you are testing with steered SIMs (where PLMN lists are set).

This is because the modem will initially look for networks predefined on the SIM. When no networks are defined in the PLMN, the modem picks the network based on signal strength instead.

Learn how leaving PLMN lists blank reduces SIM registration times: Automatic network selection.

Do not tell the modem to automatically attach.

First and foremost, sending a manual, explicit AT+COPS=0 is unadvisable, especially if you just powered on the modem.

Alright, maybe this doesn’t apply to all modems out there, but many modems may behave unexpectedly once this command is manually issued.

There’s no way to be 100% sure what happens after issuing an AT+COPS=0 command, but it's possible to imagine something like this:

Something is restarted or the modem enters a different mode when AT+COPS=0 is set.

I may be exaggerating a little bit. Nevertheless, it's better to be safe than sorry.

What actually happens during automatic attach is vaguely documented in some of ublox's AT command manuals:

Auto-registration (also sometimes called “auto-COPS”, not to be confused with automatic =0)

Yes, this is an actual quote from a manual.

In most cases, the modem will start in a mode called “auto-COPS” which is different from COPS in automatic mode.

This may be slightly confusing, but at least we’re told not to confuse those two modes.


I can imagine that all modems have this kind of mode, but many do not offer a solid documentation on how it works.


Here is the bottom line: when powered on for the first time, the modem enters an automatic selection mode where it will use the previously cached network, which is much more efficient than scanning for networks on every boot-up. If you issue the AT+COPS=0 command, the modem will enter a slightly different mode where a full scan is required.

This is not intuitive at all, but you’ll have to trust me on this (or, well, test and look at the results).

This funky behavior applies to other commands as well, but as I don’t have an exhaustive list of all commands you shouldn’t issue, I can only give the general advice of not issuing commands unless absolutely needed.

This is easy to implement in practice as all SET commands will have a corresponding READ command. In short, read the current setting and set it if it is different from the expected setting.

Good practice. The READ command is issued before any SET command is issued.


Final thoughts on AT+COPS=0 and AT+COPS=2

In general, issuing unnecessary AT commands can have drastic side-effects.

If you follow this advice, you should be able to achieve rapid attachment and lower your power consumption compared to issuing additional AT+COPS=… commands.

We hope these insights on powering modems off and on help you in your IoT development journey.

Did this answer your question?